简体   繁体   中英

django-registration registration and activation complete view NoReverseMatch

[Another Update:] I solved this and posted an answer below but if anyone has a cleaner solution that would be welcome!

[Update]: I've been able to fix the problem with registration_complete by specifying a success_url in a custom MyRegistrationView. But this doesn't work for ActivationView and it's still throwing a NoReverseMatch error while looking for a 'django_registration_activate_complete' pattern. Updated code is below.

I'm trying to implement django-registration for my webapp, and am getting NoReverseMatch errors for both my 'django_registration_activation_complete' and 'django_registration_complete' views.

When I register a new user, instead of redirecting to my 'registration_complete' view after hitting submit, I get the following NoReverseMatch error (below). The email with the activation link sends properly, and when the activation link is clicked, the user is marked as active and can sign in. However, clicking the activation link leads to a similar NoReverseMatch error, and my 'activation_complete' template doesn't show.

I have a custom user model inheriting from AbstractUser, so I had to create my own user registration form, but aside from that I don't think I'm missing anything from the django-registration quickstart guide.

My templates are all in the folder 'C:\Projects\PyDev\group_work\users\templates\django_registration' and are named as described in the quickstart guide

This is my first question so please let me know if anything is unclear or missing.

Traceback (most recent call last):
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django_registration\views.py", line 52, in dispatch
    return super().dispatch(*args, **kwargs)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django_registration\views.py", line 96, in form_valid
    return HttpResponseRedirect(self.get_success_url(self.register(form)))
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django_registration\views.py", line 93, in get_success_url
    return super().get_success_url()
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\views\generic\edit.py", line 51, in get_success_url
    if not self.success_url:
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\utils\functional.py", line 119, in __wrapper__
    res = func(*self.__args, **self.__kw)
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\urls\base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "C:\Projects\PyDev\group_work\gw_env\lib\site-packages\django\urls\resolvers.py", line 677, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'django_registration_complete' not found. 'django_registration_complete' is not a valid view function or pattern name.

urls.py:

urlpatterns = [
path('accounts/register/', RegistrationView.as_view(
    form_class=UserRegisterForm),
     name = 'django_registration_register',),
path('accounts/activate/(?P<activation_key>\w+)/$',
     ActivationView.as_view(),
     name='django_registration_activate'),
path('accounts/', 
     include('django_registration.backends.activation.urls')),
]

[Edit] in views.py (the MyActivationView doesn't solve my problem)

 class MyRegistrationView(RegistrationView):
    success_url = reverse_lazy('users:django_registration_register_complete')

class MyActivationView(ActivationView):
    success_url = reverse_lazy('login')

[Another Edit] this is also something I've tried, in urls.py, which didn't work:

path('activate/complete/', TemplateView.as_view(
    template_name = 'django_registration/activation_complete.html'
), name='django_registration_activation_complete'),

I solved this by adding the success url path to my main urls.py, instead of the one in my app:

path('activate/complete/', TemplateView.as_view(
        template_name='django_registration/activation_complete.html'
    ), name='django_registration_activation_complete'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM