自定义密码重置视图类返回ImproperlyConfigured异常

时间:2014-01-21 00:14:34

标签: django django-templates django-views

我正在尝试在password_reset中实施django.contrib.auth.views,如所示https://github.com/django/django/blob/master/django/contrib/auth/views.py#L133

但是我绝对没有运气,因为我一直跟随例外:

Exception Type: ImproperlyConfigured
Exception Value: The included urlconf tutorial.urls doesn't have any patterns in it

项目的urls.py(教程):

urlpatterns = patterns('',
    url(r'^adminp/', include('adminApp.urls')),
    url(r'^mobile/', include('mobileApp.urls')),
)

我对该应用的urls.py:

urlpatterns = patterns('',
    ....
    url(r'^login/$', UserLoginView.as_view(), name='admin_user_login'),
    url(r'^logout/$', UserLogoutView.as_view(), name='admin_user_logout'),
    url(r'^password/reset/$', UserPasswordResetView.as_view(), name='admin_password_reset'),
    url(r'^password/reset/done/$', UserPasswordResetDoneView.as_view(), name='admin_password_reset_done'),
    ....

我的班级:

class UserPasswordResetView(FormView):
    template_name = 'adminApp/registration/password_reset_form.html'
    form_class = MyPasswordResetForm
    email_template_name = 'adminApp/registration/password_reset_email.html'
    subject_template_name = 'changeMe'
    post_reset_redirect = reverse('adminApp:admin_password_reset_done')

    def form_valid(self, form):
        password_reset(self.request)
        return super(UserPasswordResetView, self).form_valid(form)

回溯:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  101.                 resolver_match = resolver.resolve(request.path_info)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in resolve
  318.             for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  346.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  341.             self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/tutorial/urls.py" in <module>
  12.     url(r'^adminp/', include('adminApp.urls')),
File "/Library/Python/2.7/site-packages/django/conf/urls/__init__.py" in include
  26.         urlconf_module = import_module(urlconf_module)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  40.         __import__(name)
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/adminApp/urls.py" in <module>
  3. from adminApp import views
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/adminApp/views.py" in <module>
  44. class UserPasswordResetView(FormView):
File "/Users/Jimbo/Dropbox/Sandbox/tutorial/adminApp/views.py" in UserPasswordResetView
  49.     post_reset_redirect = reverse('admin_password_reset_done')
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
  509.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  387.         possibilities = self.reverse_dict.getlist(lookup_view)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse_dict
  296.             self._populate()
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _populate
  262.         for pattern in reversed(self.url_patterns):
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  350.             raise ImproperlyConfigured("The included urlconf %s doesn't have any patterns in it" % self.urlconf_name)

异常类型:在/ adminp / password / reset /中配置不正确 异常值:包含的urlconf tutorial.urls中没有任何模式 现在,如果我发表评论post_reset_redirect它工作正常,那么我认为它并没有真正找到admin_password_reset_done名称?那是为什么?

更新

我根据此https://stackoverflow.com/a/7430924/531203reverse替换为reverse_lazy,但它调用了另一个错误,该错误可能在同一个域中,也可能不在同一个域中:

Exception Type: NoReverseMatch
Exception Value: Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

使用reverse_lazy时,会假定post_reset_redirect变量为none并设置:https://github.com/django/django/blob/master/django/contrib/auth/views.py#L144

1 个答案:

答案 0 :(得分:1)

你原来的问题是循环导入。 reverse_lazy解决了这个问题。

您似乎没有正确使用password_reset功能。您链接的函数期望传递关键字参数,它不会看到您的类中定义的变量。此外,您忽略了返回值 - 您应该返回它。

我不熟悉auth系统的这一部分,但这些似乎是基于函数的常规视图,所以我只想编写自己的视图并将适当的参数传递给password_reset

def my_password_reset_view(request):
    return password_reset(request,
        template_name='adminApp/registration/password_reset_form.html',
        email_template_name='adminApp/registration/password_reset_email.html',
        subject_template_name='changeMe',
        post_reset_redirect=reverse('adminApp:admin_password_reset_done'),
        password_reset_form=MyPasswordResetForm)

与其他观点类似。