70👍
The django admin uses django.contrib.messages
, you use it like this:
In your view:
from django.contrib import messages
def my_view(request):
...
if form.is_valid():
....
messages.success(request, 'Form submission successful')
And in your templates:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li {% if message.tags %} class=" {{ message.tags }} " {% endif %}> {{ message }} </li>
{% endfor %}
</ul>
{% endif %}
12👍
For Class Based Views use self.request
I also use self.request.path_info in my return
from django.contrib import messages
class MyCreateView(CreateView):
...
def form_valid(self, form):
....
self.object.save()
messages.success(self.request, 'Form submission successful')
return HttpResponseRedirect(self.request.path_info)
Same template as damio’s answer:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li {% if message.tags %} class=" {{ message.tags }} " {% endif %}> {{ message }} </li>
{% endfor %}
</ul>
{% endif %}
- [Django]-Django admin listview Customize Column Name
- [Django]-How do you know if memcached is doing anything?
- [Django]-Django – How to get admin url from model instance
7👍
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.edit import CreateView
from myapp.models import Author
class AuthorCreate(SuccessMessageMixin, CreateView):
model = Author
success_url = '/success/'
success_message = "%(name)s was created successfully"
https://docs.djangoproject.com/en/1.11/ref/contrib/messages/
- [Django]-How to get POST request values in Django?
- [Django]-Django: Rest Framework authenticate header
- [Django]-Using AuthenticationForm in Django
6👍
You don’t need to do a redirect to clear the form data. All you need to do is re-instantiate the form:
def your_view(request):
form = YourForm(request.POST or None)
success = False
if request.method == 'POST':
if form.is_valid():
form.save()
form = YourForm()
success = True
return render(request, 'your_template.html', {'form': form})
If the user refreshes the page, they’re going to initiate a GET request, and success
will be False
. Either way, the form will be unbound on a GET, or on a successful POST.
If you leverage the messages framework, you’ll still need to add a conditional in the template to display the messages if they exist or not.
- [Django]-Django aggregate or annotate
- [Django]-Logging activity on Django's admin – Django
- [Django]-Add a custom permission to a User
5👍
Django messages framework stores the messages in the session or cookie (it depends on the storage backend).
- [Django]-Django [Errno 13] Permission denied: '/var/www/media/animals/user_uploads'
- [Django]-How do I access the child classes of an object in django without knowing the name of the child class?
- [Django]-How to use Django ImageField, and why use it at all?
0👍
If you’re using a classed based view with django forms, and granted you’ve got the messages.html template set up, you can simply pass the ‘SuccessMessageMixin’ into your view like below
class YourView(SuccessMessageMixin,View):
success_message = 'your message_here'
This will display your message upon form success
- [Django]-How do I create sub-applications in Django?
- [Django]-ForeignKey to abstract class (generic relations)
- [Django]-How do I get the current date and current time only respectively in Django?