R
R
Razer15112021-02-21 15:39:07
Django
Razer1511, 2021-02-21 15:39:07

Why did the form stop working after access restriction?

Can you please tell me why the form stopped working? This is a registration form, and I have restricted access to it to authorized users. After the introduction of the dispatch function, the registration stopped working (after filling in all the fields and pressing the button, the page seems to be updated and nothing is created).
At the same time, everything is fine with the first part of the cycle (where is the redirect).

forms.py

class BaseRegisterForm(UserCreationForm):
    username = forms.CharField(label='Имя пользователя',
                               widget=forms.TextInput(
                                   attrs={'class': 'form-control',
                                          'placeholder': 'Введите имя пользователя'}))
    email = forms.EmailField(label='Электронная почта',
                             widget=forms.EmailInput(
                                 attrs={'class': 'form-control',
                                        'placeholder': 'Введите Email'}))
    password1 = forms.CharField(label='Пароль',
                                widget=forms.PasswordInput(
                                    attrs={'class': 'form-control',
                                           'placeholder': 'Введите пароль'}))
    password2 = forms.CharField(label='Подтверждение пароля',
                                widget=forms.PasswordInput(
                                    attrs={'class': 'form-control',
                                           'placeholder': 'Подтвердите пароль'}))

    class Meta:
        model = User
        fields = ["username", "email", "password1", "password2"]


views.py

class BaseRegisterView(CreateView):
    model = User
    form_class = BaseRegisterForm
    success_url = '/'

    def dispatch(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            return redirect('redirect')
        return super().get(request, *args, **kwargs)


html_template

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Продолжить">
</form>


urls.py

path('signup/', BaseRegisterView.as_view(template_name='sign/signup.html'), name='signup')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-02-21
@bacon

Now explain why you decided to specify another method in dispatch, in super, and not dispatch? Don't you understand how it works?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question