Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'),
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...