python - 在 Django 中添加路径以创建更多 View

标签 python django django-views

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

enter image description here

我的代码如下:

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/

相关文章:

python - 删除 None 值的管道

python - 使用正则表达式仅匹配特定的特殊字符

python - 我可以在自定义 Tkinter Toplevel() 窗口上获取消息框的图标 ="warning"噪音吗?

django - APIView和Model ViewSet在使用或继承上有什么区别

python - 类型错误 : '<=' not supported between instances of 'decimal.Decimal' and 'dict'

django - 自己的中间件用于限制用户 Django

python - 避免在Python中的父类中使用子方法

python - EmailMultiAlternatives 的 Celery 错误

django sudo runserver 引发错误

python - Django url 模式正则表达式将电子邮件作为 url 中的参数传递