NoReverseMatch,但在页面刷新时已解决

时间:2016-03-23 05:19:15

标签: python django django-registration

使用Django 1.8.6和Python 2.7.9,我遇到一个奇怪的现象,我的帐户激活URL正在抛出一个" NoReverseMatch"错误,但如果我刷新页面,则会解决错误并成功激活帐户。我试过删除' $'在每个网址中,我都尝试过命名空间网址,并且我尝试用模板视图替换重定向。这些都没有解决错误。

更具体地说,我收到以下错误:Reverse for 'registration_activation_complete' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

registration.backends.default.urls.py

from django.conf.urls import patterns
from django.conf.urls import include
from django.conf.urls import url
from django.conf import settings
from django.views.generic.base import TemplateView
from django.views.generic import RedirectView

from .views import ActivationView
from .views import RegistrationView


urlpatterns = patterns('',
                       url(r'^activate/complete/$',RedirectView.as_view(url='https://www.gather.kitchen/login/')),
                       url(r'^activate/(?P<activation_key>\w+)/$',
                           ActivationView.as_view(),
                           name='registration_activate'),
                       url(r'^register/complete/$',
                           TemplateView.as_view(template_name='registration/registration_complete.html'),
                           name='registration_complete'),
                       url(r'^register/closed/$',
                           TemplateView.as_view(template_name='registration/registration_closed.html'),
                           name='registration_disallowed'),
                       )

if getattr(settings, 'INCLUDE_REGISTER_URL', True):
    urlpatterns += patterns('', 
        url(r'^register/$',
            RegistrationView.as_view(),
            name='registration_register'),
    )

if getattr(settings, 'INCLUDE_AUTH_URLS', True):
    urlpatterns += patterns('', 
        (r'', include('registration.auth_urls')),
    )

registration.backends.default.views.py(ActivationView)

class ActivationView(BaseActivationView):
    def activate(self, request, activation_key):
        """
        Given an an activation key, look up and activate the user
        account corresponding to that key (if possible).

        After successful activation, the signal
        ``registration.signals.user_activated`` will be sent, with the
        newly activated ``User`` as the keyword argument ``user`` and
        the class of this backend as the sender.

        """
        activated_user = (RegistrationProfile.objects
                          .activate_user(activation_key))
        if activated_user:
            signals.user_activated.send(sender=self.__class__,
                                        user=activated_user,
                                        request=request)
        return activated_user

    def get_success_url(self, request, user):
        return ('registration_activation_complete', (), {})

registration.views.py(ActivationView)

class ActivationView(TemplateView):
    """
    Base class for user activation views.

    """
    http_method_names = ['get']
    template_name = 'registration/activate.html'

    def get(self, request, *args, **kwargs):
        activated_user = self.activate(request, *args, **kwargs)
        if activated_user:
            success_url = self.get_success_url(request, activated_user)
            try:
                to, args, kwargs = success_url
                return redirect(to, *args, **kwargs)
            except ValueError:
                return redirect(success_url)
        return super(ActivationView, self).get(request, *args, **kwargs)

    def activate(self, request, *args, **kwargs):
        """
        Implement account-activation logic here.

        """
        raise NotImplementedError

    def get_success_url(self, request, user):
        raise NotImplementedError

调试器指示错误发生在方法&#39; get&#39;在return redirect(to, *args, **kwargs)行。

此外,我传递的本地变量如下:

self: <registration.backends.default.views.ActivationView object at 0x0394A570>
args: ()
request: "<WSGIRequest\npath:/accounts/activate/686420ac51ee99765c2847b819513365d6e812fc/........etc.>"
activated_user: <MyUser: abc123@example.com>
to: 'registration_activation_complete'
success_url: ('registration_activation_complete', (), {})
kwargs: {}

还有其他类似的SO问题,我已经尝试过实施已经提出的所有解决方案。他们都没有成果。谁能告诉我我做错了什么?

0 个答案:

没有答案