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

Django访问首页出现404错误求助:URL配置问题排查

Troubleshooting Django 404 for Root URL (/)

Hey Nick, let's walk through fixing this 404 error step by step—this is super common when setting up a new Django app, so we'll get your homepage working in no time!

1. First, Check Your Project's Main urls.py

The most likely culprit is that your project's root URLconf isn't pointing to your learning_logs app's URLs. Open your main urls.py (the one in your project folder, not the learning_logs subfolder) and make sure it looks like this:

from django.contrib import admin
from django.urls import path, include  # Don't forget to import `include`!

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('learning_logs.urls')),  # This maps the root `/` to your app's URLs
]

If you're missing the path('', include('learning_logs.urls')) line, Django has no idea where to send requests to /—hence only matching the admin/ route.

2. Verify learning_logs/urls.py

Next, check your app's URLconf to ensure it's handling the root path correctly. Your learning_logs/urls.py should have a route that maps the empty string ('') to your index view:

from django.urls import path
from . import views  # Import your app's views

urlpatterns = [
    # Map the root URL to the index view
    path('', views.index, name='index'),
]

This tells Django that when it gets a request to / (after being routed from the main urls.py), it should use the index view from learning_logs.

3. Confirm Your index View Exists in learning_logs/views.py

Make sure you have an index function in your views file that renders the index.html template:

from django.shortcuts import render

def index(request):
    # Render the index template
    return render(request, 'learning_logs/index.html')

Double-check that the template path matches where your index.html is stored (more on that next).

4. Check Your Template File Structure

Django expects templates to be in a specific location for apps. Your index.html should be at:
learning_logs/templates/learning_logs/index.html

If you just put it in learning_logs/templates/ without the inner learning_logs folder, Django might not find it (especially if you have multiple apps). This can sometimes cause a 404 indirectly, or a template not found error—so it's worth verifying.

5. Quick Sanity Check: Is learning_logs Registered in settings.py?

While this isn't directly causing the URL 404, it's easy to miss. Open your project's settings.py and ensure 'learning_logs' is in the INSTALLED_APPS list:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'learning_logs',  # Your app should be here
]

Final Step: Restart the Server

After making any of these changes, restart your Django development server to apply the updates. Then visit http://localhost:8000/—it should load your index.html instead of showing the 404!

If you're still running into issues, feel free to paste the exact code from your urls.py files, views.py, and template path structure, and we can dig deeper.

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:03:08