I just installed the Django-Registration app and I have everything working except I can not figure out the password reset methods. Whenever I navigate to accounts/password/reset/ I get the following error:
Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Any ideas? Is there some problems with the django-registration urls?
Update:
I have added the following lines of code from a previous post here: Django 1.6 and django-registration: built-in authentication views not picked up
into my registration/backends/default/urls.py
url(r'^password/change/$',
auth_views.password_change,
name='password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,
name='password_change_done'),
url(r'^password/reset/$',
auth_views.password_reset,
name='password_reset'),
url(r'^password/reset/done/$',
auth_views.password_reset_done,
name='password_reset_done'),
url(r'^password/reset/complete/$',
auth_views.password_reset_complete,
name='password_reset_complete'),
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm,
name='password_reset_confirm'),
#and now add the registration urls
url(r'', include('registration.backends.default.urls')),
and then I added the following import:
from django.contrib.auth import views as auth_views
But then when I run the program after my server restart I get the following error message:
Exception Type: RuntimeError Exception Value: maximum recursion depth
exceeded while calling a Python object Exception Location:
/home/ubuntu/django-skippl/local/lib/python2.7/site-packages/Django-1.6.2-
py2.7.egg/django/utils/datastructures.py in init, line 287 Python
Executable: /home/ubuntu/django-skippl/bin/python
try this urls.py
import os
from django.contrib.auth import views as auth_views
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^accounts/', include('registration.backends.default.urls')),
#override the default urls
url(r'^password/change/$',
auth_views.password_change,
name='password_change'),
url(r'^password/change/done/$',
auth_views.password_change_done,
name='password_change_done'),
url(r'^password/reset/$',
auth_views.password_reset,
name='password_reset'),
url(r'^password/reset/done/$',
auth_views.password_reset_done,
name='password_reset_done'),
url(r'^password/reset/complete/$',
auth_views.password_reset_complete,
name='password_reset_complete'),
url(r'^password/reset/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$',
auth_views.password_reset_confirm,
name='password_reset_confirm'),
url(r'^admin/', include(admin.site.urls)),
)
This is should work :
url(r'^accounts/', include('registration.backends.simple.urls')),
url(r'^accounts/password/reset/done/$', 'django.contrib.auth.views.password_reset_done',
name='password_reset_done'),
Related
I am new to Django. I'm trying to implement jet authentication along with social authentication.
I'm following this tutorial https://jkaylight.medium.com/django-rest-framework-authentication-with-dj-rest-auth-4d5e606cde4d
I tried to implement the same but its not working.
I'm getting this error:
django.urls.exceptions.NoReverseMatch: Reverse for 'account_confirm_email' not found. 'account_confirm_email' is not a valid view function or pattern name.
My project level urls.py
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularSwaggerView
)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
# path('account/', include('allauth.urls')),
path('admin/', admin.site.urls),
path('api/user/', include('user.urls')),
path('api/schema/', SpectacularAPIView.as_view(), name='api-schema'),
path(
'api/docs/',
SpectacularSwaggerView.as_view(url_name='api-schema'),
name='api-docs'
),
]
My App level urls.py
from django.urls import path, re_path
from dj_rest_auth.registration.views import RegisterView, VerifyEmailView, ConfirmEmailView
from dj_rest_auth.views import LoginView, LogoutView
from user import views
app_name = 'user'
urlpatterns = [
path('account-confirm-email/<str:key>/', ConfirmEmailView.as_view()),
path('register/', RegisterView.as_view()),
path('login/', LoginView.as_view()),
path('logout/', LogoutView.as_view()),
path('verify-email/',
VerifyEmailView.as_view(), name='rest_verify_email'),
path('account-confirm-email/',
VerifyEmailView.as_view(), name='account_email_verification_sent'),
re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$',
VerifyEmailView.as_view(), name='account_confirm_email'),
path('listusers/', views.ListUsers.as_view(), name='list-users'),
]
When I try to register a user. It causes this error.
I'm using dj-rest-auth package to implement authentication.
If I replace the email verification related urls from app level to project level. Then everything is working fine.
What is causing this error ?
For the registration part, you have to use allauth which has some baked-in integrations with dj-rest-auth
also, you would have to add some things in your settings.py:
INSTALLED_APPS = [
"dj_rest_auth.registration",
"allauth",
"allauth.account"
]
AUTHENTICATION_BACKENDS = [
"allauth.account.auth_backends.AuthenticationBackend",
"django.contrib.auth.backends.ModelBackend",
]
add these URLs to the urls.py:
urlpatterns = [
path("signup/", include("dj_rest_auth.registration.urls"))
path("verify-email/", VerifyEmailView.as_view(), name="rest_verify_email"),
path(
"account-confirm-email/",
VerifyEmailView.as_view(),
name="account_confirm_email_sent",
),
path(
"account-confirm-email/<key>/",
VerifyEmailView.as_view(),
name="account_confirm_email",
),
]
The request is not getting <str:key>. Please check where you are supplying. It can be from view or template.
I learned tutorial Django was 3.0.0 and I am with 4.0.1 now. I try to use that module but it not working . I can see the below alert when I am trying to reset my password.
Error : Reverse for 'password_reset_confirm' not found. 'password_reset_confirm' is not a valid view function or pattern name.
My urls.py is given below
from django.conf.urls import url
from django.urls import path, reverse_lazy
from Core import views
from django.contrib.auth.views import PasswordChangeView,
PasswordResetView,
PasswordChangeDoneView,
PasswordResetConfirmView
from .forms import MyChangePasswordForm,MyPasswordResetForm
app_name ='Core'
urlpatterns = [
path('',views.home, name='home'),
path('signup/',views.SignupView.as_view(),name='signup'),
path('login/',views.LoginView.as_view(),name='login'),
path('logout/', views.LogoutView.as_view(), name='logout'),
path('change-password/', PasswordChangeView.as_view(template_name ='Core/change-password.html',form_class=MyChangePasswordForm, success_url = reverse_lazy('Core:password_change_done')), name='change-password'),
path('password-change-done/', PasswordChangeDoneView.as_view(template_name ='Core/password_change_done.html'), name='password_change_done'),
path('reset-password/',PasswordResetView.as_view(template_name = 'Core/password-reset.html',form_class=MyPasswordResetForm),name='reset-password'),
path('password-reset-confirm/<uidb64>/<token>/', PasswordResetConfirmView.as_view(success_url=reverse_lazy('Core:password_reset_confirm')),name='password_reset_confirm'),
I have been building a user account functionality for a Django app. In doing so, I have come across a problem and am uncertain of whether I am doing something wrong or have encountered an unfamiliar quirk of Django/Python. Any help is appreciated.
I have the following set of (working) urls (user_accounts/urls.py):
app_name = 'user_accounts'
urlpatterns = [
path('signup', views.UserSignUpView.as_view(), name='signup'),
path('logout', auth_views.LogoutView.as_view(), name='logout'),
path('login', auth_views.LoginView.as_view(template_name='user_accounts/login.html'), name='login'),
re_path(r'^reset/$', auth_views.PasswordResetView.as_view(template_name='user_accounts/password_reset.html', email_template_name='user_accounts/password_reset_email.html', subject_template_name='user_accounts/password_reset_subject.txt'), name='password_reset'),
re_path(r'^reset/done/$', auth_views.PasswordResetDoneView.as_view(template_name='user_accounts/password_reset_done.html'), name='password_reset_done'),
re_path(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.PasswordResetConfirmView.as_view(template_name='user_accounts/password_reset_confirm.html'), name='password_reset_confirm'),
re_path(r'^reset/complete/$', auth_views.PasswordResetCompleteView.as_view(template_name='user_accounts/password_reset_complete.html'), name='password_reset_complete')
]
I have been trying to test it as follows (user_accounts/tests/test_views_forms.py):
class SuccessfulPasswordResetTests(TestCase):
def setUp(self):
email = 'jon#doe.com'
User.objects.create_user(username='john', email=email, password='123abcdef')
url = reverse('user_accounts:password_reset')
print(reverse('user_accounts:password_reset_done'))
self.response = self.client.post(url, {'email': email})
def test_redirection(self):
'''
A valid form submission should redirect to password_reset_done
'''
url = reverse('password_reset_done')
self.assertRedirects(self.response, url)
The issue is that I get the following error:
File
"/home/user-name/sites/project-web/project/user_accounts/tests/test_views_forms.py",
line 128, in setUp
self.response = self.client.post(url, {'email': email})
django.urls.exceptions.NoReverseMatch: Reverse for
'password_reset_done' not found. 'password_reset_done' is not a valid
view function or pattern name.
Yet, when I navigate directly to /user/reset/done/ in the browser, it serves the proper template.
The project's urls.py file looks as follows. Please note that I am using DjangoCMS
from django.contrib import admin
from django.urls import include, path
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('video/', include('video_uploader.urls')),
path('user/', include('user_accounts.urls')),
path('', include('cms.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You just need to set the success_url attribute in the PasswordResetView. Out of the box it defaults to reverse_lazy('password_reset_done') but since you're using a custom app name the url can't be resolved even though the namespace is the same.
This should do the trick:
re_path(r'^reset/$', auth_views.PasswordResetView.as_view(
template_name='user_accounts/password_reset.html',
success_url=reverse_lazy('user_accounts:password_reset_done'),
email_template_name='user_accounts/password_reset_email.html',
subject_template_name='user_accounts/password_reset_subject.txt'), name='password_reset')
This is a working code. Note the addition of success_url in several paths, which end up working as a cascade. That is one gets triggered after another. I also converted from the regex format to the path format. This may have been needed to get the thing working as well.
from django.urls import path, re_path, reverse_lazy, include
from django.contrib.auth import views as auth_views
from . import views
app_name = 'user_accounts'
urlpatterns = [
path('signup/', views.UserSignUpView.as_view(), name='signup'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('login/', auth_views.LoginView.as_view(template_name='user_accounts/login.html'), name='login'),
path('password-change/', auth_views.PasswordResetView.as_view(template_name='user_accounts/password_reset.html', email_template_name='user_accounts/password_reset_email.html', subject_template_name='user_accounts/password_reset_subject.txt', success_url = reverse_lazy('user_accounts:password_reset_done')), name='password_reset'),
path('password-change/done/', auth_views.PasswordResetDoneView.as_view(template_name='user_accounts/password_reset_done.html'), name='password_reset_done'),
#re_path(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', auth_views.PasswordResetConfirmView.as_view(template_name='user_accounts/password_reset_confirm.html'), name='password_reset_confirm'),
path('password-change/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='user_accounts/password_reset_confirm.html', success_url = reverse_lazy('user_accounts:password_reset_complete')), name='password_reset_confirm'),
path('password-change/complete/', auth_views.PasswordResetCompleteView.as_view(template_name='user_accounts/password_reset_complete.html'), name='password_reset_complete')
]
I have a problem with django's view "password_reset_done".
When I try to open accounts/reset-password I have this error.
But if I open url accounts/reset-password/done it works.
URLS.PY of "accounts" app
from django.conf.urls import url
from django.contrib.auth.views import login , logout, password_reset,
password_reset_done
from . import views
urlpatterns = [
url(r'^register/$', views.register, name='register'),
url(r'^profile/$', views.profile, name='profile'),
url(r'^profile/prpage/(\d+)/$', views.profile, name='prpage'),
url(r'^profile-edit/$', views.profiledit, name='profile-edit'),
url(r'^login/$', login ,{'template_name':'accounts/login.html'},
name='login'),
url(r'^logout/$', views.logout_view, name='logout'),
url(r'^profile/(?P<proj_id>\d+)/$', views.userprojectpage,
name='userprojectpage'),
url(r'^changepassword/$', views.changepassword, name='changepassword'),
url(r'^reset-password/$', password_reset, name='reset_password'),
url(r'^reset-password/done/$', password_reset_done,
name='password_reset_done'),
]
please help! Thanks in advance)
Add success_url parameter of Class Based View PasswordResetView. This will replace default route of password_reset_done
from django.urls import path, re_path, include, reverse_lazy
path('reset/',PasswordResetView.as_view(
template_name='password_reset.html',
email_template_name='password_reset_email.html',
subject_template_name='password_reset_subject.txt',
...
success_url = reverse_lazy('accounts:password_reset_done')
...
...
),name='password_reset'),
11 there are some change made look like password_reset, password_reset_done not able to import therefore display this error regarding this url https://github.com/django/django/blob/stable/1.11.x/django/contrib/auth/views.py.
There are two way to resolve the error above.
to resolve this error add url(r'', include('django.contrib.auth.urls')), to main urls.py in your project. This will stopped the error and keep your urls in your accounts:
url(r'^reset-password/$', password_reset, name='reset_password'),
url(r'^reset-password/done/$', password_reset_done,
name='password_reset_done'),
I have resolved this error like above and it works fine.
Second way to resolve the error is different than first option above,remove the url(r'', include('django.contrib.auth.urls')), from main urls.py file and in your accounts/urls.py add following url's:
url(r'^reset-password/$', password_reset,
{'template_name':'reset_password.html',
'post_reset_redirect':'accounts:password_reset_done',
'email_template_name': 'reset_password_email.html'},
name='reset_password'),
url(r'^reset-password/done/$', password_reset_done,
{'template_name': 'reset_password_done.html'}, name='password_reset_done'),
url(r'^reset-password/confirm/(?P[0-9A-Za-z]+)-(?P.+)/$',
password_reset_confirm, {'template_name': 'reset_password_confirm.html',
'post_reset_redirect':'accounts:password_reset_complete'},
name='password_reset_confirm'),
This is the second solution and working.
I hope that helps your problem and sorry for delay respond your last comment, I was away.
DilMac
This seems like a common problem people are running into, but I can't figure it out for the life of me. I've set up a URL:
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login/$', views.login_view, name='login'),
url(r'^password_reset/$', auth_views.password_reset, name='password_reset'),
url(r'^password_reset_done/$', auth_views.password_reset_done, name='password_reset_done'),
url(r'^auth/$', views.login_auth, name='login_auth'),
url(r'^register/$', views.register_user, name='register'),
url(r'^logout/$', views.logout_user, name='logout'),
]
I try to link to the password_reset URL:
Forgot Password?
But it gives me the following error:
NoReverseMatch at /accounts/password_reset/
Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
This seems like a really simple bit of code, but it doesn't work. I can successfully call password_reset_done using this exact method, but for some reason it doesn't work with password_reset.
The password_reset line should look like this:
url(r'^password_reset/$', auth_views.password_reset, {'post_reset_redirect' : '/accounts/password_reset_done/'}, name='password_reset'),