A
A
Artur Zenkov2016-03-21 18:39:35
Django
Artur Zenkov, 2016-03-21 18:39:35

Where to convert an error into a text message?

The model has a method that processes the sent data and returns an error if something happens.
Where is the best place in the framework to translate the error code into a message for the user?
View or template?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oscar Django, 2016-03-21
@Hrom

In view via messages

A
Andrey Voskresensky, 2016-03-23
@Voskresenskyi

views.py

from django.contrib import messages

def feedback(request):
    form = FeedbackForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request, 'Ваше сообщение отправлено! Мы свяжемвся с Вами в скором времени!')
        return render(request, "feedback.html")
    context = {
        'form': form,
    }
    return render(request, 'feedback.html', context)

html
{% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question