在Django中添加路径以创建更多视图

时间:2018-01-25 12:58:06

标签: python django django-views

我正在关注Django网站上的tutorial。我尝试复制这个:

enter image description here

我的代码如下:

views.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse


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

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

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

def vote(request, film_id):
    return HttpResponse("You're commenting on film %s." % film_id)

膜/ urls.py

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

urlpatterns = [
    # url(r'^$', views.index, name='index'),
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /films/5/
    path('<int:film_id>/', views.detail, name='detail'),
    # ex: /films/5/results/
    path('<int:film_id>/results/', views.results, name='results'),
    # ex: /films/5/vote/
    path('<int:film_id>/vote/', views.vote, name='vote'),
]

有了这个,我得到了ERR_CONNECTION_REFUSED。如果我注释掉只留下索引网址的所有路径,并注释掉from django.urls import path页面,那么这就是我在尝试添加更多视图之前所处的位置。

1 个答案:

答案 0 :(得分:2)

您指的是较新的Django版本的文档,因为path()在旧版本中不存在。您可以点击右下角here右侧的Documentation version按钮选择文档版本。