python - Django 应用程序:运行服务器时出现“找不到 URL”错误
问题描述
我已经开始使用 django 学习 web 开发。我的第一个应用程序在运行命令时无法看到我的网页:
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.
解决方案
我认为这是不正确的语法,您不需要将 ^$ 与 path() 一起使用。只需使用
path("", views.index, name='index'),
path("admin/", admin.site.urls)
推荐阅读
- javascript - 如何在网站上使用机密生产密钥?
- json - 在颤振中没有得到适当的 api 共鸣
- java - @Jsonproperty 整理字段映射问题
- node.js - _id 在不同集合中的 MongoDB $lookup
- javascript - $emit 仅在第一次点击时有效
- php - 如何在自己的 docker 容器中使用 github 操作/checkout@v2
- python - Python堆栈丢失数据
- javascript - 使用 Javascript 和 xlsx.js 将多张工作表数据转换为 json 数据并在内部显示
标签
- python - 为什么我可以在 pymongo 上不使用“rds-combined-ca-bundle.pem”的情况下连接 AWS-documentDB
- internet-explorer - 当用户尝试删除任何行时,在 IE 浏览器中显示确认弹出窗口需要 5 秒的时间