You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Django官方教程项目时访问出现404 Not Found问题求助

Django Runserver正常但访问出现404 Not Found的解决方案

Hey there, let's break down why you're hitting a 404 error even though your python manage.py runserver output matches the official tutorial. This is a super common gotcha when starting out with Django—here's how to fix it:

核心原因:默认项目未配置根路径URL

When you first create a Django project, the root urls.py (in your project folder) only includes the admin route by default. Let's check that file first:

Open your project's urls.py—it should look like this:

from django.contrib import admin
from django.urls import path

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

Notice there's no entry for the root path (''), so when you visit http://127.0.0.1:8000/, Django has no idea what to serve, hence the 404.

快速验证:临时添加根视图

To confirm your server is working correctly, you can add a simple home view to the root urls.py temporarily:

from django.contrib import admin
from django.urls import path
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, Django! Your server is working!")

urlpatterns = [
    path('', home),  # Add this line for the root path
    path('admin/', admin.site.urls),
]

Save the file, refresh your browser, and you should see the "Hello, Django!" message. This confirms the server is running properly—you just need to follow the tutorial's next steps to set up your app's URLs.

按照官方教程的正确步骤配置

The official tutorial has you create an app (like polls) next—let's walk through that to get things working as intended:

  1. Create your app (if you haven't already):

    python manage.py startapp polls
    
  2. Add a URL config for your app:
    Create a urls.py file inside the polls folder, then add:

    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('', views.index, name='index'),
    ]
    
  3. Create a basic view in your app:
    Open polls/views.py and add:

    from django.http import HttpResponse
    
    def index(request):
        return HttpResponse("Welcome to the Polls app!")
    
  4. Link your app's URLs to the project:
    Update your project's root urls.py to include the polls app's URLs:

    from django.contrib import admin
    from django.urls import include, path
    
    urlpatterns = [
        path('polls/', include('polls.urls')),
        path('admin/', admin.site.urls),
    ]
    

Now, visit http://127.0.0.1:8000/polls/—you should see your "Welcome to the Polls app!" message. The root path (/) will still show a 404 for now, but that's expected—the tutorial will cover adding a root view later if needed.

额外检查点

  • Make sure your INSTALLED_APPS in settings.py includes your app (e.g., 'polls.apps.PollsConfig'). This isn't strictly required for basic views, but it's mandatory for database-related features later.
  • Double-check that you're visiting the correct URL—no typos, extra slashes, or port mismatches (Django uses 8000 by default unless you specify otherwise).

内容的提问来源于stack exchange,提问作者Вадим Васютинский

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 09:04:32