Django 名称错误

Django Name Error

我的 urls.py

中有以下代码
from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
urlpatterns = [
    url(r'^testModule/',include(testModule.urls)),
    url(r'^admin/', admin.site.urls),
]

testModule 是我的应用程序名称,其中包括:

from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name='index'),
]

我的views.py是

from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello")
# Create your views here.

但是,运行 服务器出现以下错误:

line 20: url(r'^testModule/',include(testModule.urls)), NameError: name 'testModule' is not defined

您还没有在主网址中导入 testModule

遇到了同样的问题,但是这样解决了:

  • 将 "import testModule" 添加到您的 urls.py
  • 将"from django.conf.urls import url"添加到你的urls.py应用程序目录中,在本例中为testModule

完成后,您将在重新加载 127.0.0.1:8000 时收到 "Page not found at/" 错误。这是因为您的应用程序 url 实际上位于 127.0.0.1:8000/testModule