“编写您的第一个Django应用程序”教程中的404错误

时间:2015-05-27 20:58:30

标签: python django

我是编程新手,并且一直致力于编写1.8版的“编写你的第一个Django App”教程。我遇到了错误。

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

我见过其他人有类似的错误,但没有看到解决方案。我甚至只是简单地复制和粘贴本节中文件的教程(第3部分的顶部),但仍然没有看到解决方案。

MYSITE \轮询\ urls.py

from django.conf.urls import url

from . import views

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

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/', include(admin.site.urls)),
]

MYSITE \轮询\ views.py

from django.http import HttpResponse


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

MYSITE \ settings.py

...
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'
...

我不知道断开连接的位置,但我根据错误的猜测是它与主mysite目录中的urls.py文件有关...它似乎没有读取民意调查的urlpatterns?这怎么可能是resolvd? (我应该注意在浏览器中对url进行民意调查后,我在使用和不使用最终的“/”时尝试过此操作。)

1 个答案:

答案 0 :(得分:19)

根网址应该是mysite\mysite\urls.py,而不是mysite\urls.py