【问题标题】:Custom password reset view class returning ImproperlyConfigured exception自定义密码重置视图类返回 ImproperlyConfigured 异常
【发布时间】:2014-01-21 00:14:33
【问题描述】:

我正在尝试在django.contrib.auth.views 中实现password_reset,如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/531203,我将reverse 替换为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 变量为无并设置:https://github.com/django/django/blob/master/django/contrib/auth/views.py#L144

【问题讨论】:

  • 粘贴的urls.pytutorial.urls
  • @user995394,它是我称之为 app1 的 urls.py,它有类。
  • 我认为问题出在您的网址配置中的其他地方。异常消息中提到的tutorial.urls 在哪里?由于reverse(),您在该行看到了问题,它会遍历您的所有网址并且通常可以发现问题。不一定和admin_password_reset_done有任何关系。
  • 凯文,请查看我的最新更新。我根本无法确定 urls.py 文件中的任何错误。基本上我已经注释掉了所有内容以确保它并不能解决问题。当我使用reverse_lazy 时,它实际上会使用默认值覆盖post_reset_redirect 变量:github.com/django/django/blob/master/django/contrib/auth/…

标签: django django-templates django-views


【解决方案1】:

您最初的问题是循环导入。 reverse_lazy 已修复。

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

我不熟悉这部分身份验证系统,但这些似乎是常规的基于函数的视图,所以我会自己编写并将适当的参数传递给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)

与其他视图类似。

【讨论】:

  • 你能给我一个如何调用函数的例子吗?
  • 是否也可以使用基于类的视图?
  • 当然,但您不会真正使用 CBV 的任何功能。您可以从View 继承,并像上面一样从get()post() 调用password_reset。或者你可以覆盖dispatch()
  • 我意识到这很麻烦,所以我改用函数方法。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 2019-05-07
  • 2016-12-22
  • 2019-01-16
  • 2020-06-23
相关资源
最近更新 更多