How to redirect twice in Django?
So I have a main page (/Main), and a page that allows users to get in touch with me (lets call it /Contact).
After users submit the form successfully in Contact, I want to redirect them to Main and show a small box at the top of the page that says "form submitted successfully". I've been able to do this, by redirecting to a URL that looks something like /Main/Success which is basically the Main view with a success parameter passed in.
The problem is, I don't want the URL in the user's browser to show /Main/Success. I want it to look like /Main in the URL address box, even though the user's seeing an extra box on the top of the page. This is to prevent people from simply typing /Main/Success in the address bar and going to the page with the box on top. Users should ONLY see the success box after they have submitted the form successfully. Also, when the user refreshes the Main page with the success box on it, it should simply show the Main page again without the success box.
Does this mean I should be redirecting twice? from /Contact to /Main/Success to /Main again? And if I do this, how do I get the box to display on Main? Any ideas how to approach this problem? I've been thoroughly confused since last time and will really appreciate any help I can get.
Thanks!
Take a look at the Django messages framework. It allows you to define just this kind of message and avoids multiple redirects.
All you have to do, is add some code in your template that will display messages (if there are any) like this:
{% if messages %}
{% for message in messages %}
{{ message }}
{% endfor %}
{% endif %}
and then you can "send" message to the user in your view like this:
from django.contrib import messages
messages.success(request, 'Form submitted successfully')
For more details (eg which kinds of messages there are and what attributes the message in the template has) check out the documentation mentioned above.
链接地址: http://www.djcxy.com/p/89118.html上一篇: 不要让网站自动重定向Scrapy
下一篇: 如何在Django中重定向两次?