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

Django 2.0.4 URL路由失效求助:按官方教程配置仍无法解析

Fixing Django URL Routing Issues for Your Polls App

Hey there! The most likely missing piece here is that you haven't connected your polls app's URLconf to your main project's URL configuration (the incomplete whyitsnotworking/urls.py file you mentioned). Let's walk through exactly what you need to add:

Step 1: Update Your Main Project's URLs

Open whyitsnotworking/urls.py and ensure it includes the polls app's URLs using Django's include function. Here's the complete setup you need:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    # Routes any URL starting with /polls/ to your polls app's URLconf
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),
]

The include() function tells Django to delegate all requests to URLs starting with /polls/ to the polls/urls.py file you already configured. Without this line, Django has no way to map incoming requests to your polls views.

Step 2: Double-Check These Quick Validation Points

  • Confirm your app is installed: Make sure polls is added to the INSTALLED_APPS list in whyitsnotworking/settings.py:
    INSTALLED_APPS = [
        'polls',  # This line is easy to miss!
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    
  • Restart your dev server: Any changes to URL configuration files require restarting the Django development server to take effect.
  • Verify your access URL: With the setup above, you need to visit http://localhost:8000/polls/ (not the root / URL) to see your "Hello, world" message. If you want the root URL to point directly to polls, you can adjust the main route to r'^' instead of r'^polls/'.

Once you make these adjustments, your URL routing should work as expected!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:22:07