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

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.

Related

How to display a different page when user refreshes the current page in Django

I am a beginner in Django and I want to display page-y when the user refreshes the page-x
this event not related to django if you want to do that you should trigger refresh page event using JavaScript
and when this event happen you can make JavaScript function run and make this function navigate user to new page or open new window
check this article to better understanding what I mean
best of luck
Pure HTML/ Django answer. Reload in a Web browser will do another GET for the page. If the Django server remembers the context of the last GET, it can choose to do a redirect rather than show the same again. So for example, if it's something in the user's profile:
if request.user.profile.whatever:
return redirect( 'myapp:anotherpage' )

Is there any way to know if a python webbrowser page has been responded to?

I'm using python to display a web page with webbrowser like this:
url = "file://c/python27/debug.html"
webbrowser.open_new_tab(url)
The user then enters some values on the page and clicks submit. At that point I need my python script to do another function call. How do I know the user has clicked submit on the page that I displayed?
If not, is there a better way to display the html page so I CAN know that it was responded to with a callback or something?

POST parameters python

I need to view post parameters with python. The website in question has a post parameter of business_number where number changes after each successful action. I need to be able to get this number and then submit a POST request with a value.
A GET request does not give me the needed information. (I have tried with requests.) I have used a firefox addon called httprequester and used the "Submit" button and within the response is the information I need. I'm a real novice with HTTP :) so is there a way I can submit with Python (click the submit button) and then save the response in a variable that I will sort through.
I used the requests module to solve my problem:
http://docs.python-requests.org/en/latest/

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/

saving event of opening url in database

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>

Categories