M
M
Mike2017-03-17 17:56:39
Django
Mike, 2017-03-17 17:56:39

I have a class for authorization. How can I make my error messages in it?

# for Authentication
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login

class LoginFormView(FormView):
    form_class = AuthenticationForm
    template_name = "fighter/login.html"
    success_url = "/demo"

    def form_valid(self, form):
        self.user = form.get_user()
        login(self.request, self.user)
        return super(LoginFormView, self).form_valid(form)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-03-17
@google_online

Add a form_invalid() method to the view , and add an error to the form using the add_error() method :

def form_invalid(self, form):
    form.add_error(None, u'Вы кто такие? Я вас не звал!');
    return super(LoginFormView, self).form_invalid(form)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question