我正在使用 django-registration 并在我的 settings.py 中将登录和注销 URL 设置为
LOGIN_URL = '/users/login/'
LOGOUT_URL = '/users/logout/'
在我设置的 urls.py 中
url(r'^users/', include('registration.backends.default.urls', namespace='users')),
一切正常,除了当我尝试访问 localhost:8000/users/passwords/reset 时出现错误
NoReverseMatch at /users/password/reset/
Reverse for 'django.contrib.auth.views.password_reset_done' with arguments '()' and keyword arguments '{}' not found.
但是,当我将此行添加到 urls.py 文件时,一切正常
(r'^users/', include('registration.auth_urls')),
这个解决方案让我感到困惑,因为 Registration.backends.default.urls 包含行
(r'', include('registration.auth_urls')),
它应该与添加到我的项目的 urls.py 文件中的行执行相同的操作。为什么将这一行添加到项目的 urls.py 文件中可以修复错误?
最佳答案
您正在通过包含提供一个命名空间
参数。除其他外,这会导致您在模板中使用 reverse()
函数或 {% url %}
时还必须提供命名空间。
参见https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces欲了解更多信息。
关于python - Django:django 注册反向查找失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195628/