我在 Django 中進行登錄和注冊,但出現(xiàn)以下錯誤。請幫我解決它。文件“/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函數(shù)在views.
添加回答
舉報
0/150
提交
取消