我正在关注tutorial在 Django 网站上。我尝试复制这个:
我的代码如下:
View .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 消息。如果我注释掉所有路径,仅留下索引 url,并注释掉 from django.urls import path
,则会显示一个页面,但这就是我在尝试添加更多 View 之前所在的位置。
最佳答案
您指的是较新 Django 版本的文档,因为旧版本中不存在 path()
。您可以通过点击右下角的文档版本
按钮来选择文档版本here .
关于python - 在 Django 中添加路径以创建更多 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48443470/