Reputation: 53
I'm new in using django templating/frontend and I would like to redirect to a page with a custom message in django.
Here's what I've tried but I can't make It work.
View
return redirect("/login",custom_error="Try Again Later")
Login.html
{% if custom_error %}
<p class="errornote">
{{ custom_error }}
</p>
{% endif %}
Any suggestions on how to make this work?
Upvotes: 2
Views: 5564
Reputation: 476537
Please do not encode error messages in the URL. You can use the Django message framework [Django-doc] for this.
Before you make the request, you add a message:
from django.contrib import messages
messages.error('Try again later')
return redirect('name-of-login-view')
It is furthermore better to specify the name of the view, instead of the path, since the path might later change.
In the template(s), probably best in a base.html
template that is inherited by (most) other templates, you can then render this with:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
Upvotes: 3