8๐
The best way would probably be to use a coded querystring on the redirect URLโฆ its an old school approach.
You could do something like
/page/?m=1, /page/?m=2, etc
You would then parse that variable with request.GET in the view code and show the appropriate message.
75๐
For the sake of completion and future reference, you can now use the messages framework. After you install it:
views.py
from django.contrib import messages
def view(request):
# your code
messages.success(request, "Your data has been saved!")
HttpResponseRedirect(request.path)
template.html
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
- [Django]-Mixin common fields between serializers in Django Rest Framework
- [Django]-Django Deprecation Warning or ImproperlyConfigured error โ Passing a 3-tuple to django.conf.urls.include() is not supported
- [Django]-Django โ The included urlconf doesn't have any patterns in it
14๐
if you are using auth and have a logged in user you could:
http://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.models.User.message_set.create
GET params are also hackable. The querystring, as mentioned in other answers, could be used.
I think the most preferred way would be to use the sessions framework. That way you can load up whatever you want in the context and get
{{ request.session.foo }}
foo
could be the message or you could do:
{% ifequal request.session.foo 1 %} Nice work! {% else %} Almost! {% endifequal %}
and other fun stuff.
http://docs.djangoproject.com/en/dev/topics/http/sessions/#using-sessions-in-views
- [Django]-How to define two fields "unique" as couple
- [Django]-Gunicorn Connection in Use: ('0.0.0.0', 5000)
- [Django]-How to check Django version
8๐
You canโt. HttpResponseRedirect sends a client-side redirect (HTTP status code 302) to the browser, and then the browser re-requests another page.
You can set a URL query string on the redirect, though that will be visible to the user and anyone intercepting HTTP requests (i.e. proxies), and is therefore not suitable for sensitive information.
- [Django]-How do you set DEBUG to True when running a Django test?
- [Django]-Django โ limiting query results
- [Django]-How to make Django serve static files with Gunicorn?
6๐
From your views.py you hast have to put a key/value-pair into the session and then read it from the HTML template.
For example:
views.py
# your code here
request.session['vote'] = 1
return HttpResponseRedirect(request.path)
your_template.html
{% ifequal request.session.vote 1 %}
<!-- Your action here -->
{% endifequal %}
- [Django]-How can I use Django permissions without defining a content type or model?
- [Django]-How to annotate Count with a condition in a Django queryset
- [Django]-Find object in list that has attribute equal to some value (that meets any condition)
1๐
The only way I know of to pass any data with a redirect is to add GET parameters to the URL youโre passing in. To avoid XSS hacks youโd want to pass a specific constant like:
[current path youโre passing in]?message=saved
And then process the message=saved parameter in the handler for the path you passed in.
A somewhat more complicated way would be not passing the data in the redirect, and instead using something like http://code.google.com/p/django-notify/ to store session-based data that is displayed to the user following the redirect.
- [Django]-Can I use a database view as a model in Django?
- [Django]-FileUploadParser doesn't get the file name
- [Django]-Django select only rows with duplicate field values
0๐
You add ?saved=1 to the query string and check for it with something like:
saved = request.GET.get('saved', False)
- [Django]-How to use regex in django query
- [Django]-How do I check for last loop iteration in Django template?
- [Django]-Django urls without a trailing slash do not redirect