Django 2.0.4 URL路由失效求助:按官方教程配置仍无法解析
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
pollsis added to theINSTALLED_APPSlist inwhyitsnotworking/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 tor'^'instead ofr'^polls/'.
Once you make these adjustments, your URL routing should work as expected!
内容的提问来源于stack exchange,提问作者Dexter Lakin

