Почему моя форма не действительна при просмотре, что бы я ни вводил?

Я пытаюсь создать приложение аутентификации с формой, но оно не работает. Я поместил некоторые функции печати в мое представление, и оно печатает пост, но не действительный. Я хочу нажать кнопку submit, которая вызовет запрос post и представление создаст нового пользователя.

view.py

def index(request):
    form = forms.RegisterForm()
    context = {
        'form': form
    }

    if request.POST:
        print('post')
        if form.is_valid():
            print('valid')
            form.save()
            email = form.cleaned_data.get('email')
            raw_password = form.cleaned_data.get('password')
            account = authenticate(email=email, password=raw_password)
            print('authenticate')
            login(request, account)
            return redirect('home')
        else:
            print('not valid')
            context['registration_form'] = form
    else:
        form = forms.RegisterForm()
        context['registration_form'] = form

    return render(request, 'index.html', context)

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Submit</button>
    </form>
</body>
</html>

forms.py

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import ReadOnlyPasswordHashField

User = get_user_model()

class RegisterForm(forms.ModelForm):
    """
    The default 

    """
    full_name = forms.CharField(max_length=30)
    password = forms.CharField(widget=forms.PasswordInput)
    password_2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['email']

    def clean_email(self):
        '''
        Verify email is available.
        '''
        email = self.cleaned_data.get('email')
        qs = User.objects.filter(email=email)
        if qs.exists():
            raise forms.ValidationError("email is taken")
        return email

    def clean(self):
        '''
        Verify both passwords match.
        '''
        cleaned_data = super().clean()
        full_name = cleaned_data.get('full_name')
        password = cleaned_data.get("password")
        password_2 = cleaned_data.get("password_2")
        if password is not None and password != password_2:
            self.add_error("password_2", "Your passwords must match")
        return cleaned_data


class UserAdminCreationForm(forms.ModelForm):
    """
    A form for creating new users. Includes all the required
    fields, plus a repeated password.
    """
    password = forms.CharField(widget=forms.PasswordInput)
    password_2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['email']

    def clean(self):
        '''
        Verify both passwords match.
        '''
        cleaned_data = super().clean()
        password = cleaned_data.get("password")
        password_2 = cleaned_data.get("password_2")
        if password is not None and password != password_2:
            self.add_error("password_2", "Your passwords must match")
        return cleaned_data

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password"])
        if commit:
            user.save()
        return user


class UserAdminChangeForm(forms.ModelForm):
    """A form for updating users. Includes all the fields on
    the user, but replaces the password field with admin's
    password hash display field.
    """
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = User
        fields = ['email', 'password', 'is_active', 'admin']

    def clean_password(self):
        # Regardless of what the user provides, return the initial value.
        # This is done here, rather than on the field, because the
        # field does not have access to the initial value
        return self.initial["password"]

Если вы хотите, чтобы я опубликовал что-нибудь еще, просто напишите в комментариях

Вы не получили form's data в своем представлении с Post запросом.

Это должно исправить это:

def index(request):
    #first goes get request and it creates blank form
    if request.method == "GET":
        form = forms.RegisterForm()
        context = {
            'form': form
        }
        return render(request, 'index.html', context)
    
    # second, from get request goes post request with data inside
    elif request.method == "POST":
        #at this line you recieve forms data with request.POST
        form = forms.RegisterForm(request.POST)
        #and here you validate it
        if form.is_valid():
            form.save()
            email = form.cleaned_data.get('email')
            raw_password = form.cleaned_data.get('password')
            account = authenticate(email=email, password=raw_password)
            print('authenticate')
            login(request, account)
            return redirect('home')
        else:
            print('not valid')
            context['registration_form'] = forms.RegisterForm()
            
        return render(request, 'index.html', context)

Эта строка исправит ваш код:

form = forms.RegisterForm(request.POST)

Если вы добавите его перед if form.is_valid(): (но я также отредактировал ваш код (не обязательно)).

Вернуться на верх