🛠️ Part 3:【邂逅Django】—— (三)视图
🛠️ Part 4:【邂逅Django】—— (四)表单和通用视图
🛠️ Part 5:【邂逅Django】—— (五)完善界面(自定义界面和样式)
🛠️ Part 6:【邂逅Django】—— (六)自定义管理界面
🛠️ Part 7:【邂逅Django】—— (七)自动化测试
本系列文章,在 Django
官方文档教程的基础模板下,进行了一定的改进和删除,添加了一些自己的见解。
希望大家看完该系列文章后,对 Django
能够有一个清晰的认识。
路漫漫兮其修远兮,吾将上下而求索!
❗ ❗ ❗ 学习过程中,多看官方文档,可以解决很多问题 ❗ ❗ ❗
Django
中的视图概念是【一类具有相同功能和模板的网页集合】。比如,在一个博客应用中,可能会创建如下视图:
- 博客首页 —— 展示最近的几项内容
- 内容详情页 —— 详细展示某项内容
- 以年为单位的归档页 —— 展示选中的年份里各个月份创建的内容
- 以月为单位的归档页 —— 展示选中的月份里各天创建的内容
- 以天为单位的归档页 —— 展示选中天里创建的所有内容
- 评论处理器 —— 用于响应内容添加评论的操作
而在投票应用中,需要以下几个视图:
- 问题索引页 —— 展示最近的几个投票问题
- 问题详情页 —— 展示某个投票的问题和不带结果的选项列表
- 问题结果页 —— 展示某个投票的结果
- 投票处理器 —— 用于响应用户为某个问题投票的操作
在 Django
中,网页和其他内容都是从视图派生而来的。每一个视图表现为一个 Python
函数(对于基于类的视图来说,表现为一个方法)。 Django
会根据用户请求的 URL
来选择使用哪个视图。
为了将 URL
和视图关联起来, Django
使用 URLconfs
来配置。 URLconf
将 URL
映射到视图。
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
def detail(request, question_id):
return HttpResponse(f"You're looking at question {question_id}")
def results(request, question_id):
response = "You're looking at the results of question {}"
return HttpResponse(response.format(question_id))
def vote(request, question_id):
return HttpResponse("You're voting on question %s." % question_id)
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("/", views.detail, name="detail"),
path("/results/", views.results, name="results"),
path("/vote/", views.vote, name="vote"),
]
启动 Django
开发服务器,可以在浏览器输入以下 URL
进行查看:
每个视图要做两件事:返回一个包含被请求页面内容的 HttpResponse
对象,或者抛出一个异常。
视图可以从数据库里读取记录,可以使用一个模板引擎,或者其他能用 Python
完成的事。
pools/views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by("-pub_date")[:5]
output = ", ".join([q.question_text for q in latest_question_list])
return HttpResponse(output)
def detail(request, question_id):
return HttpResponse(f"You're looking at question {question_id}")
def results(request, question_id):
response = "You're looking at the results of question {}"
return HttpResponse(response.format(question_id))
def vote(request, question_id):
return HttpResponse(f"You're voting on question {question_id}")
在查看之前,可以创建一些 Question
的数据。
启动 Django
服务器,在浏览器输入 http://127.0.0.1:8000/polls/
进行查看。
🌊 5.1 模板目录说明
首先,在 polls
目录下创建一个 templates
目录。 Django
会在这个目录里查找模板文件。
在 settings.py
配置文件中, TEMPLATES
配置项描述了 Django
如何载入和渲染模板。默认设置了 DjangoTemplates
后端,并将 APP_DIRS
设置为 True
。这一选项将会让 DjangoTemplates
在每个 INSTALLED_APPS
文件夹下寻找 templates
子目录。
这就是为什么没有进行 DIRS
的设置, Django
也能正确找到 polls
的模板位置的原因。
说明:虽然可以将模板文件直接放在 polls/templates
文件夹中(而不是再建立一个 polls
子文件夹),但是这样做不太好。 Django
会选择第一个匹配的模板文件,如果有一个模板文件和另一个应用中的目标文件重名, Django
没有办法区分它们。我们需要 Django
选择正确的模板,最好的方法就是把它们放入各自的命名空间中,也就是把这些模板放入一个和自身应用重名的子文件夹里。
🌊 5.2 创建模板文件
// polls/templates/polls/index.html
{% if latest_question_list %}
{% for question in latest_question_list %}
{{ question.question_text }}
{% endfor %}
{% endif %}
polls/templates/polls/detail.html
{{ question.question_text }}
{% for choice in question.choice_set.all %}
{{ choice.choice_text }}
{% endfor %}
🌊 5.3 使用模板
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from .models import Question
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))
🌊 5.4 使用模板(一个快捷函数): render()
from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader
from .models import Question
def index(request):
latest_question_list = Question.objects.order_by("-pub_date")[:5]
context = {"latest_question_list": latest_question_list}
return render(request, "polls/index.html", context)
使用 render
,不再需要导入 loader
和 HttpResponse
。不过如果还有其他函数(比如 detail
、 results
和 vote
)需要用到的话,就需要保持 HttpResponse
的导入。
🌊 5.5 抛出404错误
polls/views.py
from django.http import HttpResponse, Http404
from django.shortcuts import render
from django.template import loader
from .models import Question
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, "polls/detail.html", {"question": question})
一个快捷函数: get_object_or_404()
polls/views.py
from django.http import HttpResponse, Http404
from django.shortcuts import render, get_object_or_404
from django.template import loader
from .models import Question
def detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, "polls/detail.html", {"question": question})
在 polls/index.html
中,链接硬编码:
{{ question.question_text }}
问题在于,硬编码和强耦合的链接,对于一个包含很多应用的项目来说,修改起来十分困难。然而,在 polls.urls
的 url()
函数中,通过 name
参数为 URL
定义了命名,可以使用 {% url %}
标签代替它:
{{ question.question_text }}
这个标签的工作方式是,在 polls.urls
模块的 URL
定义中寻找具有指定名字的条目。
如果想改变投票详情视图的 URL
,比如想改成 polls/specifics/12/
,不用在模板里修改任何东西(包括其他模板),只要在 polls/urls.py
里稍微修改以下就可以:
path("specifics//", views.detail, name="detail")
在一个真实的 Django
项目中,可能会有五个、十个、二十个,甚至更多应用。 Django
如何分辨重命名的 URL
呢?
在根 URLconf
中添加命名空间。在 polls/urls.py
文件中稍作修改,加上 app_name
设置命名空间:
polls/urls.py
from django.urls import path
from . import views
app_name = "polls"
urlpatterns = [
path("", views.index, name="index"),
path("/", views.detail, name="detail"),
path("/results/", views.results, name="results"),
path("/vote/", views.vote, name="vote"),
]
现在编辑 polls/index.html
,修改:
{{ question.question_text }}
修改为指向具有命名空间的详细视图:
{{ question.question_text }}
本文简单介绍了 Django
模板、自定义模板,以及模板和 URL
之间的映射关系。
Original: https://www.cnblogs.com/CoderChaos/p/16444750.html
Author: CoderChaos
Title: 【邂逅Django】——(三)视图
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/713258/
转载文章受原作者版权保护。转载请注明原作者出处!