Django URLConf无法正确解决

时间:2017-06-13 03:06:59

标签: python django

我正在关注Django入门教程,我遇到了一个奇怪的错误。或者至少我认为这是一个奇怪的错误 我在part 3,正在撰写更多观点。我尽我所知,遵循教程中的信件。

我的/polls/urls.py文件如下所示:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^(?P<question_id>[0-9]+)/results/$', views.results, 
        name='results'),
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

if __name__ == "__main__":
    pass

polls/views.py看起来像这样:

from django.http import HttpResponse


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


def detail(request, question_id):
    return HttpResponse("You're looking at question {question}.".format(question=question_id))


def results(request, question_id):
    response = "You're looking at the results of question {question}.".format(question=question_id)
    return HttpResponse(response)


def vote(request, question_id):
    return HttpResponse("You're voting on question {question}.".format(question=question_id))


if __name__ == "__main__":
    pass

我在my_project/urls.py注册了网址:

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

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

如果我去http://127.0.0.1:8000/polls我会看到我希望看到的“hello world”消息,但是我尝试查找其中一个问题,即我转到http://127.0.0.1:8000/polls/1/我看到以下内容错误讯息:

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

1. ^admin/
2. ^polls ^$ [name='index']
3. ^polls ^(?P<question_id>[0-9]+)/$ [name='detail']
4. ^polls ^(?P<question_id>[0-9]+)/results/$ [name='results']
5. ^polls ^(?P<question_id>[0-9]+)/vote/$ [name='vote']

The current URL, polls/1/, didn't match any of these.

我的网址与3号网不匹配怎么可能?这是一个基本的正则表达式。 enter image description here

2 个答案:

答案 0 :(得分:2)

问题在于您的cur row equals last row + 1,在if((direction == 'H' && curRow == lastRow && curCol == lastCol + 1) || (direction == 'V' && curRow == lastRow + 1 && curCol == lastCol)) { $(this).addClass("highlightE"); lastRow = curRow; lastCol = curCol; } 之后,您错过了my_project/urls.py,请更改为:

polls

答案 1 :(得分:0)

您可以使用Django APPEND_SLASH设置(请参阅here获取文档)或修改您的网址格式,以使/成为可选设置,例如像这样:

url(r'^(?P<question_id>[0-9]+)/?$', views.detail, name='detail'),