Я пытаюсь повторно использовать приложение account
(управляет подпиской / входом / регистрацией с использованием представлений на основе классов), которое я создал в одном проекте, в другом проекте.
account/urls.py
:
from django.urls import path
from django.contrib.auth import views as auth_views
app_name = 'account'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name = 'login'),
path('logout/', auth_views.LogoutView.as_view(), name = 'logout'),
path('password_change/', auth_views.PasswordChangeView.as_view(), name = 'password_change'),
path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name = 'password_change_done'),
# reset password urls:
path('password_reset/', auth_views.PasswordResetView.as_view(), name = 'password_reset'),
path('password_reset/done', auth_views.PasswordResetDoneView.as_view(), name = 'password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name = 'password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name = 'password_reset_complete'),
]
Если я попытаюсь получить доступ к любому из этих URL-адресов, появится следующая ошибка:
NoReverseMatch at /account/<url>/
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
In template /Users/justinobrien/Desktop/recipeSite/website/templates/base.html, error at line 12
Единственная строка, которая изменится, - это NoReverseMatch at /account/<url>/
.
Я не упоминаю представление входа в систему в моей очень примитивной базе. html, поэтому я не уверен, откуда это (файл в project_root/templates/base.html
):
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Website</title>
{% block head %}
{% endblock %}
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
Другие файлы, которые, на мой взгляд, могут быть важными: project_root/urls.py
:
from django.conf.urls import url
from django.urls import path, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
# user account related urls:
path('account/', include('account.urls')),
]
и кое-что, что я добавил в settings.py
: изменено DIRS
в переменной TEMPLATES
, чтобы Django мог найти мои base.html
file
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
и я также добавил:
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
Я пытался повторно использовать эту функцию, поскольку она работала в предыдущем проекте, но мне кажется, что мне не хватает шагнуть куда-нибудь. Любая помощь очень ценится.
edit: Я понял, что существует также множество файлов шаблонов account
, из которых могла возникнуть эта ошибка?
Ошибка трассировки полного стека:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/account/logout/
Django Version: 3.0.6
Python Version: 3.6.1
Installed Applications:
['account.apps.AccountConfig',
'crispy_forms',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
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']
Template error:
In template /Users/justinobrien/Desktop/recipeSite/website/templates/base.html, error at line 0
Reverse for 'login' not found. 'login' is not a valid view function or pattern name.
1 :
2 : {% load static %}
3 : <!DOCTYPE html>
4 : <html lang="en">
5 :
6 : <head>
7 : <title>Website</title>
8 :
9 :
10 : {% block head %}
Traceback (most recent call last):
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 143, in _get_response
response = response.render()
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/response.py", line 105, in render
self.content = self.rendered_content
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/response.py", line 83, in rendered_content
return template.render(context, self._request)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 936, in render
bit = node.render_annotated(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/base.py", line 903, in render_annotated
return self.render(context)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/template/defaulttags.py", line 443, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/urls/base.py", line 87, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/justinobrien/Desktop/recipeSite/env/lib/python3.6/site-packages/django/urls/resolvers.py", line 677, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /account/logout/
Exception Value: Reverse for 'login' not found. 'login' is not a valid view function or pattern name.