Django教程 - 第1部分 - URL不匹配

时间:2021-08-06 10:43:59

This has been asked before, but the problems were usually with placement of the mysite/urls.py or missing text somewhere. I've gone over those in detail, but doesn't apply here. I'm following the django tutorial EXACTLY, which means it hasn't referenced including the polls app in the settings.py file. I can pull up the right view if I manually type in the "polls" at the end of the url, as in "http://127.0.0.1:8000/polls" but I shouldn't have to do this for it to work. I'm also assuming the tutorial isn't wrong in some way. Link to the tutorial is: https://docs.djangoproject.com/en/1.9/intro/tutorial01/

之前已经问过这个问题,但问题通常是放置mysite / urls.py或丢失文本。我已经详细介绍了这些内容,但不适用于此处。我正在关注django教程,这意味着它没有引用包括settings.py文件中的民意调查应用程序。如果我手动输入网址末尾的“民意调查”,我可以拉出正确的视图,如“http://127.0.0.1:8000/polls”,但我不应该这样做工作。我也假设教程在某种程度上没有错。链接到教程是:https://docs.djangoproject.com/en/1.9/intro/tutorial01/

The error I get:

我得到的错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, , didn't match any of these.

My views.py file:

我的views.py文件:

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

def index(request):
    return HttpResponse("Hello, world.  You're at the polls index.")

My polls/urls.py file:

我的polls / urls.py文件:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
]

My mysite/mysite/urls.py file:

我的mysite / mysite / urls.py文件:

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

urlpatterns = [
    url(r'^polls/', include('polls.urls')),
    url(r'^admin/', admin.site.urls),

]

This is the tree for the mysite/mysite directory just to show that I am in the right folder (there is no separate urls.py file in the main mysite directory):

这是mysite / mysite目录的树,只是为了表明我在正确的文件夹中(主mysite目录中没有单独的urls.py文件):

.
├── __init__.py
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── settings.cpython-35.pyc
│   ├── urls.cpython-35.pyc
│   └── wsgi.cpython-35.pyc
├── settings.py
├── urls.py
└── wsgi.py

Again, best I can tell this is to the letter following the django tutorial guidance. I'd like to fix it, but more importantly understand why it isn't working.

再说一次,我最好能说出这是django教程指导之后的字母。我想解决它,但更重要的是要理解为什么它不起作用。

1 个解决方案

#1


0  

Have you checked the INSTALLED_APPS variable in the settings file to include the 'polls' app?

您是否检查过设置文件中的INSTALLED_APPS变量以包含“民意调查”应用程序?

#1


0  

Have you checked the INSTALLED_APPS variable in the settings file to include the 'polls' app?

您是否检查过设置文件中的INSTALLED_APPS变量以包含“民意调查”应用程序?