NameError:名称“views”未在 django 2.2.3 和 python 3.7.0

我在 Django 中进行登录和注册,但出现以下错误。请帮我解决它。


文件“/home/mritunjay/project/pr/fitbit/accounts/urls.py”,第 7 行,在路径('',views.home,name =“home”)中,NameError:名称'views'未定义


这里urls.py (app)


from django.urls import path


from accounts.views import home, SignUpView


app_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 render


from django.urls import reverse_lazy


from django.views.generic import CreateView


from accounts.forms import SignUpForm


from django.contrib.auth.models import User



class 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 admin

from django.urls import path, include


from django.contrib.auth import views as auth_views


from django.views.generic import TemplateView


#from accounts import urls


urlpatterns = [


    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)中的代码


请帮助我。


qq_遁去的一_1
浏览 79回答 1
1回答

慕尼黑的夜晚无繁华

您正在引用视图而不导入它。在urls.py(应用程序)中试试这个from django.urls import pathfrom accounts import viewsapp_name = "accounts"urlpatterns = [    path('', views.home, name="home"),    path('signup/', views.SignUpView.as_view(), name='signup'),    path('login/', views.login, name='login'),    ]注意:确保login函数在views.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python