django password reset NoReverseMatch error

2019-07-18 02:04发布

问题:

I'm developing a Django app and I'm having trouble setting up reset password. I'm getting the following error:

Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': 'MQ', u'token': u'49v-cabad3fe98f5d9f64377'}' not found. 0 pattern(s) tried:

This is coming in my password_reset_email.html file which is:

Someone asked for password reset for email {{ email }}. Follow the link below:
{{ protocol}}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

I have the following in urls.py

(r'^registration/password_reset_confirm', 'django.contrib.auth.views.password_reset_confirm',
    {'template_name' : 'coursework/password_reset_confirm.html',
     'set_password_form' : SetPasswordForm,
     'post_reset_redirect' : 'coursework.views.list_comments'}),
(r'^registration/password_reset_done', 'django.contrib.auth.views.password_reset_done',
    {'template_name' : 'coursework/index.html'}),
(r'^registration/password_reset', 'django.contrib.auth.views.password_reset',
    {'password_reset_form' : PasswordResetForm,
     'template_name' : 'coursework/reset_password.html',
     'email_template_name' : 'coursework/password_reset_email.html',
     'subject_template_name' :'coursework/password_reset_subject.txt',
     'post_reset_redirect' : 'coursework.views.list_comments'}),

Clearly I'm doing something daft but I cannot work it out.

STACK TRACE ADDED

Environment:


Request Method: POST
Request URL: http://localhost:23080/registration/password_reset

Django Version: 1.9
Python Version: 2.7.11
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'coursework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')



Traceback:

File "C:\FCA\lib\django\core\handlers\base.py" in get_response
  134.                 resolver_match = resolver.resolve(request.path_info)

File "C:\FCA\lib\django\core\urlresolvers.py" in resolve
  374.             for pattern in self.url_patterns:

File "C:\FCA\lib\django\utils\functional.py" in __get__
  33.         res = instance.__dict__[self.name] = self.func(instance)

File "C:\FCA\lib\django\core\urlresolvers.py" in url_patterns
  417.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "C:\FCA\lib\django\utils\functional.py" in __get__
  33.         res = instance.__dict__[self.name] = self.func(instance)

File "C:\FCA\lib\django\core\urlresolvers.py" in urlconf_module
  410.             return import_module(self.urlconf_name)

File "C:\Python27\lib\importlib\__init__.py" in import_module
  37.     __import__(name)

Exception Type: SyntaxError at /registration/password_reset
Exception Value: invalid syntax (urls.py, line 24)

回答1:

Your pattern does not accept uidb64 and token arguments. Change it to:

(r'^registration/password_reset_confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', 'django.contrib.auth.views.password_reset_confirm',
 {'template_name' : 'coursework/password_reset_confirm.html',
  'set_password_form' : SetPasswordForm,
  'post_reset_redirect' : 'coursework.views.list_comments'},
 name='password_reset_confirm'),


回答2:

I'm posting this answer to maybe help others with the same problem. I was getting the same error and did all the suggested solutions on the internet, with no luck!

at the end, thanks to this link's help, it worked!

solution: add url(r'^', include('django.contrib.auth.urls')), to the mainProject>urls.py