saving event of opening url in database - python

Imagine post with url as a content.
Simplifying:
<post><link>http://blablabla.com/</link></post>
I'm looking for a magic function which allow me saving in database which url from posts has been clicked. For instance someone clicks the url and not only it opens in new window but also backround magic function saves this event in database. Can I the url and def at once? I have an app build in django. If there is anybody who knows how to bite it? If yes, could you help me?

I would say you have two options here. If it's an internal link (i.e. http://blablabla.com refers to your own site) you could have the logging functionality built into the receiving view.
Alternatively, you could modify the link to go through your own script which logs the request (urlencoded) before redirecting to the requested page (after decoding):
<post><link>http://blablabla.com/</link></post>
becomes
<post><link>http://mysite.com/log?page=http%3A%2F%2Fblablabla.com%2F</link></post>

Related

Execute .py programme from HTML button

I want to execute a python programme whenever the user clicks on any of the buttons from the django submit_line.html page.
I have tried using variations of the following:
onclick="python script.py"
onclick="/usr/bin/python /home/django/project/script.py"
onclick="python ../../script.py"
but I can't get the script to run.
Does anyone know of any solutions to my problem?
Thanks for your time.
If instantly: use javascript.
If by refreshing the page: just use requests and write your Python code as function in views.py
You don't seem to have understood anything from the Django tutorial or documentation.
In Django, you access the server-side functionality via URLs, which are served by views. You don't reference a Python script directly in your template, you reference a URL which maps to a view, and your Python code goes in that view.
Quite apart from that, onclick is Javascript. You can't just reference a Python script, that makes no sense. You can write a Javascript function to call (via Ajax) a URL which does what you want, or more simply make it a link to that URL.

Use django view without redirecting or refreshing a page

I have a button next to some text on a page. I'd like to execute some Python code if that button is pressed. The only way I know how to do this is through a view. The thing is, I need the page not to redirect or refresh or anything, just some code to execute when the button is pressed. Any ideas on how I'd get this done?
Asynchronous Javascript and XML (Ajax).
You can perform an Ajax request to the view you want to execute and this will be done without refreshing nor redirecting the current web page.
This is the W3C Ajax tutorial which might be good for beginners. All you have to do is code some javascript in your templates, add the onclick event listener to your button and you're up :)
You may take a look at this tutorials and documentation, seems very apropiate.
Hope this helps!
When button is clicked you can use javascript to make an async request (AJAX) to your django project
typing in django ajax to google shows a lot of resources, one of which is a beginners tutorial:
http://lethain.com/two-faced-django-part-5-jquery-ajax/

How to: Click button, then do stuff, then show confirmation in Django

Basic question, but I can't find a simple answer anywhere.
What is the best way to do the following in Django:
User pushes button
Some Python code gets crunched server-side
User sees confirmation page
All the user needs to know is that the server did what he told it to. No input other than the button click.
Thanks in advance for your help.
That's what a Form is for. You can create a small HTML form with just the button.
The form's action is the URI to which a request is sent.
Django parse the URI and calls a view function.
The function does the work and returns the resulting page.

Facebook calling Google App Engine code using GET instead of POST

I've been developing a Facebook app using Google App Engine in Python and the pyfacebook bindings. For weeks everything worked fine but suddenly it stopped.
At first I thought it was a code change so I rolled back the entire dev directory to a version I knew worked, but still it failed. It's possible a change I made to the application's settings caused the issue but, if so, I can't figure out what.
I've figured out that the problem is that instead of calling the post(self) method of my Main class, Facebook is calling using a GET.
Does anyone know why Facebook would use a GET method instead of a POST? It's an IFrame app.
Thanks,
The typical flow for a user when using the application begins with the user landing at some Canvas URL, like http://apps.facebook.com/runwithfriends/. At this point, Facebook will load up it's chrome, and render a tag to your application. You'll notice there isn't a src specified. Using some JavaScript and the tag, Facebook triggers a POST request to your application. This is done for security reasons, as the sensitive user data won't be sent via the HTTP Referrer header as long it's sent as POST data.
Although I'm not completely sure this was the cause, it appears I changed from an FBML app to an IFrame app. FBML mode relies on POST calls but IFrame appears to use GET. I'm inferring this answer from what I read here as well as from the observations I'm seeing and this being the only answer that makes any sense.

Showing processing message in Python

I want to show the processing information or log in the original page when the submitted request is being served until it completes the execution. I thought it would be meaningful to the user to know what is happening behind the request.
I don't find a clue to do so though, can you guys help me out as how people are doing like this one below one - for your reference
http://www.xml-sitemaps.com/
there are two ways i could imagine handling this:
have your backend script (python) output the information of a long process to a log of some sort (text file, database, session, etc...) and then have javascript grab the information via ajax and update the current page.
same deal, but instead of ajax just have a meta refresh on the page which would grab the latest updated information.
you may use python threading, which will create a new process in background
and display your messages on that thread
hope it helps ;)

Categories