Any /polls URL always call index() function in views.py

匆匆过客 提交于 2019-11-27 08:48:15

问题


polls/urls/py


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

urlpatterns = [

url('', views.index, name='index'),
url('<int:question_id>/', views.detail, name='detail'),

# ex: /polls/5/results/
url('<int:question_id>/results/', views.results, name='results'),
# ex: /polls/5/vote/
url('<int:question_id>/vote/', views.vote, name='vote'),
]


views.py

from __future__ import unicode_literals
from django.http import HttpResponse
from .models import Question
from django.template import loader

# from django.shortcuts import render

def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:5]
template = loader.get_template('polls/index.html')
context = {
    'latest_question_list': latest_question_list,
}
return HttpResponse(template.render(context, request))


def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)


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


def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)

url=http://127.0.0.1:8000/polls

url=http://127.0.0.1:8000/polls/1/

url=http://127.0.0.1:8000/polls/1/results

These all urls are giving same mapping same function index(). Any help will be appreciated


回答1:


Firstly, it's really important to make sure that you are following the tutorial that matches your version of Django. Here are the links for Django 2.0 and Django 1.11.

You are getting the unexpected behaviour because you are mixing the old url and new path syntax. If you are using Django 2.0, change the import and update your URL patterns:

from django.urls import path

urlpatterns = [

    path('', views.index, name='index'),
    path('<int:question_id>/', views.detail, name='detail'),

    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),

]

If you are using an earlier version of Django, you need to use regexes instead. For example, the Django 1.11 tutorial gets you to write:

from django.conf.urls import url

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



回答2:


Which version of Django is this? Your url patterns look like a mixture of 2.0 and older routing.

Your empty url pattern would probably match anything so not another route ever gets called.

For Django 2.0 + do what @Alasdair suggest above for older Django version something like below:

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


来源:https://stackoverflow.com/questions/49087449/any-polls-url-always-call-index-function-in-views-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!