"This field is required" before submitting form

class CustomPasswordChangeView(PasswordChangeView):
    template_name = 'change_password.html'  
    success_url = reverse_lazy('profile')

    def form_valid(self, form):
        response = super().form_valid(form)
        messages.success(self.request, "password changed successfully!")
        return response

I have no idea why this is happening, I open or reload the page and these errors appear even without pressing the submit button.

How are you navigating to this page?

Yes, there is a reset password button that calls this view and thinking about it now I think that maybe this is the problem because the button itself has a post method (perhaps to send the user?), but how to resolve these fields with errors?

<form action="{% url 'password_change' %}" method="post">
        {% csrf_token %}
        <button type="submit" class="edit-button">Change Password</button>
</form>

But even without pressing the button, just reloading and the errors appear too.

Yes, if you’re clicking a button that submits a POST request to that view, that would cause this error.

Note, if this is the complete form tag, it’s not sending any data. You could replace that with a get.

If you called a POST method to navigate to the page, then a “reload” is going to resend the POST.

Thank you very much, I had no idea that reloading would redo the post. I put it as get and it resolved.

Although I resolved this error now I’m stuck on another one(programmer’s life). If I don’t take too much advantage of you, could I ask?

We’re here to help. We’re all volunteers here, answering questions as time, knowledge, and energy permits. You’re always free to ask - and where possible, you’ll even get an answer!

I really admire your work…

I’m trying to reset the password for those who forgot their password.
When I press the submit button to change the password, instead of being redirected to success url auth/profile/,
the redirect does this crazy path: /accounts/login/?next=/auth/profile/

“GET /accounts/login/?next=/auth/profile/ HTTP/1.1” 404 2595

#accounts.views
class CustomPasswordResetConfirmView(PasswordResetConfirmView):
    template_name = 'custom_password_reset_confirm.html'
    success_url = reverse_lazy('profile')
      
    def form_valid(self, form):
        messages.success(self.request, 'Password Changed.')
        return super().form_valid(form)
#accounts.urls
urlpatterns = [
  
    ...
    path('reset-password/', PasswordResetView.as_view(), name='password_reset'),
    path('reset-password/confirm/<uidb64>/<token>/', CustomPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    ...
    path('profile/', profile, name='profile'),

]

#mainapp
urlpatterns = [
    path('admin/', admin.site.urls,name='admin'),
    path('', HomeListView.as_view(), name='home'),
    path('auth/', include('accounts.urls')),
    path('home/', include('home.urls')),
]

The strangest thing is that shortly after, I am automatically logged out, but the password is changed, which means that the email that sends the encrypted user is working.

By default, the PasswordResetConfirmView does not result in the user being logged in. See the docs for PasswordResetConfirmView.post_reset_login.

If your profile/ url requires the user be logged in, then yes, they would need to be redirected to the login page before they can be sent to the profile page.

1 Like

Is possible to change this so it doesn’t log out the user?

That view is not the view intended to be used by someone who is logged in and is looking to change their password.

That view is designed to be used by someone who can’t log in because they forgot their password and need to reset it.

You can have this view log you in by setting the post_reset_login attribute to True for the view.

1 Like

I managed to solve it thanks to your help, just leave the parameter as true. I wanted to do this because the default django view to change asks for an old password, and it can happen that the user is logged in but forgets it.