I have a view which does the certain task and return to another view which render hello.html template.
def 1stview(request): #Do this #Do that return HttpResponseRedirect('/success/') def success(request): return render_to_response('overview.html', {'overview_files': b, 'total_files':total_files, 'total_size':total_size, 'username': username}, context_instance=RequestContext(request))
After successfully completing 1st view I want to pass 'Successful' message in overview.html
. There are lots of redirect to success
view. I want to transfer message only when going through 1st view. How can I do that?
HttpResponseRedirect is a subclass of HttpResponse (source code) in the Django web framework that returns the HTTP 302 status code, indicating the URL resource was found but temporarily moved to a different URL. This class is most frequently used as a return object from a Django view.
shortcuts and for redirection to the Django official website we just pass the full URL to the 'redirect' method as string, and for the second example (the viewArticle view) the 'redirect' method takes the view name and his parameters as arguments.
The render function Combines a given template with a given context dictionary and returns an HttpResponse object with that rendered text. You request a page and the render function returns it. The redirect function sends another request to the given url.
Adding a more elaborative answer.
1: Configure a message storage in your settings.py:
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
or if you are not using sessions, use CookieStorage:
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.CookieStorage'
2: In your view, import django.contrib.messages:
from django.contrib import messages
3: Set the message data before returning the HttpResonse:
messages.success(request, 'Changes successfully saved.')
which is a shorthand for:
messages.add_message(request, messages.SUCCESS, 'Changes successfully saved.')
The message tags (messages.SUCCESS in this case) can then be used in your template to i.e. add a corresponding CSS-class or hide debug-messages. Django includes a few by default but if you wish to use this with Bootstrap's default alert classes you will need to add some custom message tags for the missing ones.
4: In your template you can then use the messages like this if you are using Bootstrap alerts:
{% if messages %} {% for message in messages %} <div class="alert {% if message.tags %}alert-{{ message.tags }}{% endif %}" role="alert">{{ message }}</div> {% endfor %} {% endif %}
For example, Django uses 'error' as the default tag for ERROR while Bootstrap uses danger to indicate errors. The best solution is to use custom tags, but you can also monkeypatch it in your template (ugly solution):
{% if messages %} {% for message in messages %} <div class="alert {% if message.tags %}alert-{% if message.level == DEFAULT_MESSAGE_LEVELS.ERROR %}danger{% else %}{{ message.tags }}{% endif %}{% endif %}" role="alert">{{ message }}</div> {% endfor %} {% endif %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With