我在 Django 中进行登录和注册,但出现以下错误。请帮我解决它。文件“/home/mritunjay/project/pr/fitbit/accounts/urls.py”,第 7 行,在路径('',views.home,name =“home”)中,NameError:名称'views'未定义这里urls.py (app)from django.urls import pathfrom accounts.views import home, SignUpViewapp_name = "accounts"urlpatterns = [ path('', views.home, name="home"), path('signup/', SignUpView.as_view(), name='signup'), path('login/', views.login, name='login'), ]这里views.py (app)from django.shortcuts import renderfrom django.urls import reverse_lazyfrom django.views.generic import CreateViewfrom accounts.forms import SignUpFormfrom django.contrib.auth.models import Userclass SignUpView(CreateView): success_url = reverse_lazy('login') template_name = 'templates/signup.html'def home(request): return render(request, "home.html", {})这里是urls.py(项目)from django.contrib import adminfrom django.urls import path, includefrom django.contrib.auth import views as auth_viewsfrom django.views.generic import TemplateView#from accounts import urlsurlpatterns = [ path('admin/', admin.site.urls), path('', include('accounts.urls')), # Main Page path('', TemplateView.as_view(template_name='home.html'), name='home'), # Login and Logout path('login/', auth_views.LoginView.as_view(redirect_authenticated_user=True, template_name='templates/login.html'), name='login'), path('logout/', auth_views.LogoutView.as_view(next_page='home'), name='logout'),]这些是 **NameError: name 'views' is not defined ** 显示在urls.py(app)中的代码请帮助我。
1 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
您正在引用视图而不导入它。在urls.py(应用程序)中试试这个
from django.urls import path
from accounts import views
app_name = "accounts"
urlpatterns = [
path('', views.home, name="home"),
path('signup/', views.SignUpView.as_view(), name='signup'),
path('login/', views.login, name='login'),
]
注意:确保login函数在views.
添加回答
举报
0/150
提交
取消