使基于内置类的视图知道Django

时间:2019-01-01 12:48:30

标签: django django-templates django-views

因此,我正在尝试使用基于类的内置视图(PasswordChangeView和PasswordChangeDoneView)实现密码更改功能。 现在,当我在内置模板中输入旧密码,新密码并确认密码字段并按Enter(或提交)时,出现“没有反向匹配”错误,提示“未找到'password_change_done'的反向密码。“'password_change_done'不是有效的视图函数或模式名称。”我做了很多研究,发现这是因为我正在使用应用程序名称空间(urls.py中的app_name ='authenticationApp'),这就是django无法找到相反方向的原因。我的简单问题是:如何让内置视图知道其应该在反向函数中使用命名空间的URL?

PS:我知道使用自己的模板将解决此问题。 编辑1:

Environment:


Request Method: POST
Request URL: http://localhost:8000/password_change/

Django Version: 2.1.1
Python Version: 3.6.7
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'authenticationApp.apps.AuthenticationappConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Traceback:

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
  142.                     response = view_func(request, *args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
  45.         return bound_method(*args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  21.                 return view_func(request, *args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/contrib/auth/views.py" in dispatch
  338.         return super().dispatch(*args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch
  88.         return handler(request, *args, **kwargs)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/views/generic/edit.py" in post
  142.             return self.form_valid(form)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/contrib/auth/views.py" in form_valid
  350.         return super().form_valid(form)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/views/generic/edit.py" in form_valid
  57.         return HttpResponseRedirect(self.get_success_url())

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/views/generic/edit.py" in get_success_url
  51.         if not self.success_url:

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/utils/functional.py" in __wrapper__
  108.                 res = func(*self.__args, **self.__kw)

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/urls/base.py" in reverse
  90.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))

File "/home/python/Desktop/environments/second/lib/python3.6/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  622.         raise NoReverseMatch(msg)

Exception Type: NoReverseMatch at /password_change/
Exception Value: Reverse for 'password_change_done' not found. 'password_change_done' is not a valid view function or pattern name.

1 个答案:

答案 0 :(得分:1)

在主urls.py(位于settings.py旁边)中,您应该这样导入视图:

path('password_change/', views.PasswordChangeView.as_view(template_name='your_template.html'), name='password_change'),  # or your path
path('password_change/done/', views.PasswordChangeDoneView.as_view(), name='password_change_done'),