代码之家  ›  专栏  ›  技术社区  ›  Dan

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

  •  1
  • Dan  · 技术社区  · 8 年前

    我正在跟踪 tutorial 在Django网站上。我尝试复制以下内容:

    enter image description here

    我的代码如下:

    意见。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)
    

    电影/URL。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\u CONNECTION\u拒绝。如果我注释掉所有路径,只留下索引url,并注释掉 from django.urls import path 将显示一个页面,但这就是我在尝试添加更多视图之前所处的位置。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Laurynas Tamulevičius    8 年前

    您参考的是Django新版本的文档 path() 旧版本中不存在。您可以通过单击 Documentation version 右下角的按钮 here .