django 2.0.4网址不起作用

时间:2018-04-08 15:33:58

标签: python django django-views

Django突然似乎没有正确处理网址。我按照"第3部分写了你的第一个Django app"再次只有一个民意调查视图和urlsconf。它不起作用。我错过了什么?

这是我的views.py:

from django.http import HttpResponse


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

这是我的民意调查\ urls.py:

from django.conf.urls import url

from . import views

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

whyitsnotworking \ 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)),
]

whyitsnotworking \ settings.py:

INSTALLED_APPS = [
    'polls',

以下是我的民意调查应用的目录:     迁移     admin.py     apps.py     models.py     tests.py     urls.py     views.py     的初始化的.py

我可以运行测试服务器:

Run 'python manage.py migrate' to apply them.
April 08, 2018 - 16:25:27
Django version 2.0.4, using settings 'whyitsnotworking.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[08/Apr/2018 16:25:33] "GET / HTTP/1.1" 200 16348
Not Found: /polls
[08/Apr/2018 16:25:39] "GET /polls HTTP/1.1" 404 1964

但是请收到以下错误消息:

Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/polls

Using the URLconf defined in whyitsnotworking.urls, Django tried these URL     patterns, in this order:

    admin/

The current path, polls, didn't match any of these.

2 个答案:

答案 0 :(得分:2)

移除admin.site.urls周围的include(),因为您在admin.site中调用了指向网址模块的直接pythonic路径,因此您无需使用include()。如果您想告诉Django您想要包含的include()文件的路径,那么您只需要使用urls.py,这通常是一个字符串,其格式与<appname>.urls类似。< / p>

答案 1 :(得分:0)

问题是我在mysite \ urls.py中有url文件,而不是mysite \ mysite \ urls.py

多么虚伪!