解析“GET/blog HTTP/1.1”404 2065(Python Django教程:功能齐全的Web应用程序第2部分应用程序和路由)

2024-04-26 00:22:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在YouTube上学习这个教程https://www.youtube.com/watch?v=a48xeeo5Vnk&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p&index=2。你知道吗

我收到错误消息[24/Oct/2019 22:41:41] "GET /blog HTTP/1.1" 404 2065。你知道吗

urls.py

from django.urls import path
from .import views
urlpatterns = [
    path('', views.home, name='blog-home'),
    ]



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

def home(request):
    return HttpResposne('<h1>Blog Home</h1>')

# Create your views here.


urls.py - django_project1

"""django_project1 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include('blog.urls')),
]

我已尝试清除历史记录、缓存并重新启动服务器。我试着找出我可能打错的地方。关于类似问题的其他问题不是指我正在关注的教程系列,就是指本系列后面的一集。你知道吗


Tags: topathdjangonamefrompyimportadd
2条回答

试试这个,因为我总是使用“/”作为主页,而且从不只使用空格网址.py你知道吗

from django.urls import path
from .import views
urlpatterns = [
    path('/', views.home, name='blog-home'),
    ]

如果这是代码的1:1,则应检查:

return HttpRe**spos**ne('<h1>Blog Home</h1>')

因为这里有输入错误;) 我也经常在我的应用程序URL中使用app\u name,例如:

from django.urls import path, re_path, include
from . import views

app_name = 'myApp'
urlpatterns = [ ... ]

另外,请检查您的应用程序是否已添加到设置.py,在“已安装的应用程序”列表中

相关问题 更多 >