Reputation: 855
I am trying to create a change-password and change-username system for my users, however when I try to visit the page, I get a 404 page not found
urls.py:
urlpatterns = [
path('forget/', views.forget, name='forget'),
path('', views.login_user, name='login'),
path('home/', views.home, name='home'),
path('logout/', views.logout_view, name='logout'),
path('register/', views.register_view, name='register'),
path('edit-register/', views.edit_register_view, name='edit_register'),
path('change-password/', views.password_change, name='password_change'),
path('reset-password/', views.PasswordReset.as_view(), name='password_reset'),
path('reset-password-done/', views.PasswordResetDone.as_view(), name='password_reset_done'),
path('reset-password/<uidb64>/<token>/', views.PasswordResetConfirm.as_view(), name='password_reset_confirm'),
path('reset-password-complete/', views.PasswordResetComplete.as_view(), name='password_reset_complete'),
]
views.py:
def forget(request):
msg = None
# Get userid from session
userid = request.session.get('userid')
print(userid)
# Get user object from User database
user = get_object_or_404(User, id=userid)
if request.method == 'POST': # check for the post request body
form = ForgetForm(request.POST, instance=user)
if form.is_valid():
user = form.save()
print(form.cleaned_data.get('password1'))
msg = 'user password updated'
return redirect('/login')
else:
msg = 'form is not valid'
else:
form = SignUpForm()
return render(request, 'forget.html', {'form': form, 'msg': msg})
forms.py:
class forgetForm(forms.Form):
username = forms.CharField(
widget=forms.TextInput(
attrs={
"class": "form-control"
}
)
)
password1 = forms.CharField(
widget=forms.PasswordInput(
attrs={
"class": "form-control"
}
)
)
password2 = forms.CharField(
widget=forms.PasswordInput(
attrs={
"class": "form-control"
}
)
)
Let me know if further information is required.
Upvotes: 0
Views: 471
Reputation: 10699
The error suggests that the user with that ID wasn't found when it executed this line.
user = get_object_or_404(User, id=userid)
You are getting the user ID from the stored session:
userid = request.session.get('userid')
Perhaps you haven't correctly populated the value for that. Make sure that it was correctly saved for the current session ID. You can verify this by checking the sessionid
in the cookies (it should be visible in the request headers section in the browser) and looking at its values in the table django_session
in your database. The key userid
must be there.
An alternative is to try accessing instead request.user
specifically request.user.id
. Note that request.user
is also populated based on session data.
If everything above fails, verify the user ID really exists in your user database.
Upvotes: 1