我对django相对较新,并尝试创建一个包含用户名和pw栏的登录表单的主页。我试图在我自己的项目中重新创建我在这里看到的内容:
但我一直回来 NameError:name' url'没有定义。我对导致这种情况的原因很感兴趣。我最初写入项目级别的urls.py,但后来转移到应用程序级别(帐户)urls.py,因为这对我有意义......但我对此没有信心。
以下是我的文件:
success
项目/ settings.py
project
├── manage.py
├── db.sqlite3
├── templates
├── accounts
| ├──migrations
| ├──_pycache_
| ├──admin.py
| ├──apps.py
| ├──models.py
| ├──_init__.py
| ├──urls.py
| ├──tests.py
| └──views.py
└── project
├── settings.py
├── urls.py
└── wsgi.py
项目/ urls.py
from django.urls import reverse_lazy
import os
...
SITE_ID = 1
LOGIN_URL = reverse_lazy('login')
LOGIN_REDIRECT__URL = reverse_lazy('home')
LOGOUT_REDIRECT__URL = '/'
enter code here
账户/ urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/', include('accounts.urls')),
path('accounts/', include('allauth.urls')),
path('', include('posts.urls')),
]
账户/ views.py
from django.urls import path
from . import views
urlpatterns = [
path('signup/', views.SignUpView.as_view(), name='signup'),
url(r'^accounts/', HomeView.as_view(template_name='../templates/home.html', name='home')),
url(r'^accounts/login/$', 'django.contrib.auth.views.login', name='login'),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout', name='logout'),
]
模板/ home.html的
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
class SignUpView(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
class HomeView(TemplateView):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(HomeView, self).dispatch(*args, **kwargs)
答案 0 :(得分:0)
您需要在accounts / urls.py中导入网址:
from django.conf.urls import url