Created 2 forms on the test site - registration and authorization. For example, if in the registration form you skip some field, then an error is displayed that you need to fill in the field. If you make a mistake in the authorization field, the page is simply updated with the fields reset. Both forms function correctly. Django 1.9.7

from django.shortcuts import render, redirect from django.contrib.auth import authenticate, login, logout from django.contrib.auth.forms import UserCreationForm, AuthenticationForm from .models import Post def auth(request): if request.user.is_authenticated(): return redirect('/') elif request.method == 'POST': form_auth = AuthenticationForm(request.POST) username, password = request.POST.get('username', ''), request.POST.get('password', '') user = authenticate(username=username, password=password) if user is not None: login(request, user) return redirect("/") else: form_auth = AuthenticationForm() return render(request, "blog/auth.html", {'form_auth': form_auth}) def reg(request): if request.user.is_authenticated(): return redirect('/') elif request.method == 'POST': form_reg = UserCreationForm(request.POST) if form_reg.is_valid(): new_user = form_reg.save() return redirect('/auth/') else: form_reg = UserCreationForm() return render(request, "blog/reg.html", {'form_reg': form_reg}) 

    1 answer 1

    In order for the form to generate a list of errors, it is necessary to run its validation. In the registration view, you have a call to the is_valid () method of the form, which fills the errors form attribute with values.

    Authentication occurs within the AuthenticationForm form.

     def auth(request): if request.user.is_authenticated(): return redirect('/') elif request.method == 'POST': form_auth = AuthenticationForm(request=request, data=request.POST) if form_auth.is_valid(): user = form_auth.get_user() login(request, user) return redirect("/") else: form_auth = AuthenticationForm() return render(request, "blog/auth.html", {'form_auth': form_auth}) 
    • With this option, even the authorization stopped working. Errors also do not appear. - Michael
    • It is more logical to validate the login and password in the form. There is also user authentication, there is no need to call authenticate in view. Please post a template, maybe there’s a problem. - Chikiro
    • You can see what lies in form_auth.errors, to understand that this form does not return errors, or the template does not display them. - Chikiro
    • The pattern is all right. Look at the corrected code, the request is now passed to the form as the first argument. - Chikiro