Answer the question
In order to leave comments, you need to log in
How to display messages?
There is an abstract example. As a result, you need to output success_message to the template. But the end result is a blank page. All imports are present. Tell me what's wrong?
models.py:
class Author(models.Model):
name = models.CharField(max_length=200)
url(r'^example/$', AuthorCreate.as_view()),
class AuthorCreate(SuccessMessageMixin, ListView):
model = Author
template_name = "example.html"
success_message = "Hello world"
{% if messages %}
{% for message in messages %}
<p{% if message.tags %} class="{{message.tags}}"{% endif %}>{{message}}</p>
{% endfor %}
{% endif %}
Answer the question
In order to leave comments, you need to log in
Have you looked at the source at all? If so, here they are:
class SuccessMessageMixin(object):
"""
Adds a success message on successful form submission.
"""
success_message = ''
def form_valid(self, form):
response = super(SuccessMessageMixin, self).form_valid(form)
success_message = self.get_success_message(form.cleaned_data)
if success_message:
messages.success(self.request, success_message)
return response
def get_success_message(self, cleaned_data):
return self.success_message % cleaned_data
https://docs.djangoproject.com/en/1.8/ref/class-ba...
class AuthorCreate(SuccessMessageMixin, ListView):
model = Author
template_name = "example.html"
def get_context_data(self, **kwargs):
context = super(AuthorCreate, self).get_context_data(**kwargs)
context['some_text_to_tpl'] = "Hello world"
return context
#html
{{ some_text_to_tpl }}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question