任何/polls URL 总是在 views.py 中调用 index() 函数 [英] Any /polls URL always call index() function in views.py

查看:16
本文介绍了任何/polls URL 总是在 views.py 中调用 index() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这些所有的 url 都给出了相同的映射相同的函数 index().任何帮助将不胜感激

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

推荐答案

首先,确保您遵循与您的 Django 版本相匹配的教程确实很重要.以下是 Django 2.0Django 1.11.

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.

您得到了意外行为,因为您混合了旧的 url 和新的 path 语法.如果您使用的是 Django 2.0,请更改导入并更新您的 URL 模式:

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

]

如果您使用的是较早版本的 Django,则需要改用正则表达式.例如,Django 1.11 教程让您编写:

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

这篇关于任何/polls URL 总是在 views.py 中调用 index() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆