Django应用程序:运行服务器时出现“找不到URL”错误

时间:2020-01-11 05:41:40

标签: python django web-development-server

我已经开始使用django学习网络开发人员。我的第一个应用程序运行命令时无法看到我的网页:

python manage.py runserver

这是我的代码段:

#view.py(in app folder)
from django.shortcuts import render
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello World!")

#urls.py (in project folder)
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
    path('^$',views.index,name='index'),
    path('^admin/', admin.site.urls),
]

我已经创建了环境并也激活了它。

这是我的网页屏幕上的输出。

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Using the URLconf defined in first_project.urls, Django tried these URL patterns, in this order:

^$ [name='index']
^admin/
The empty path didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to 
False, and Django will display a standard 404 page.

1 个答案:

答案 0 :(得分:0)

我认为语法不正确,您不需要在path()中使用^ $。 只需使用

path("", views.index, name='index'),
path("admin/", admin.site.urls)