学学笔记 - 官方 Qiter | 学习笔记

Django4 - 第一个项目案例代码 第三节

发布时间:9个月前热度: 147 ℃评论数:

何为视图

Django 中的视图的概念是「一类具有相同功能和模板的网页的集合」
在我们的投票应用中,我们需要下列几个视图:

  • 问题索引页——展示最近的几个投票问题。
  • 问题详情页——展示某个投票的问题和不带结果的选项列表。
  • 问题结果页——展示某个投票的结果。
  • 投票处理器——用于响应用户为某个问题的特定选项投票的操作。

网页和其他内容都是从视图派生而来。每一个视图表现为一个 Python 函数
Django 将会根据用户请求的 URL 来选择使用哪个视图
URL 样式是 URL 的一般形式 - 例如:/newsarchive/<year>/<month>/

编写视图

举例子说明

polls/views.py 里添加更多视图

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

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

def vote(request, question_id):
    return HttpResponse("You're voting on question %s." % question_id)

把这些新视图添加进 ​polls.urls​ 模块里,只要添加几个 ​url()​ 函数调用就行:

from django.urls import path

from . import views

urlpatterns = [
    # ex: /polls/
    path('', views.index, name='index'),
    # ex: /polls/5/
    path('<int:question_id>/', views.detail, name='detail'),
    # ex: /polls/5/results/
    path('<int:question_id>/results/', views.results, name='results'),
    # ex: /polls/5/vote/
    path('<int:question_id>/vote/', views.vote, name='vote'),
]

浏览器,如果你转到 “/polls/5/“ ,Django 将会运行 detail() 方法
“/polls/5/results/“ 和 “/polls/5/vote/“ ——你将会看到暂时用于占位的结果和投票页

实际视图

展示数据库里以发布日期排序的最近 5 个投票问题,以空格分割:

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)

# Leave the rest of the views (detail, results, vote) unchanged

这里有个问题:页面的设计写死在视图函数的代码里的。如果你想改变页面的样子,你需要编辑 Python 代码。所以让我们使用 Django 的模板系统,只要创建一个视图,就可以将页面的设计从代码中分离出来。
首先,在你的 ​polls ​目录里创建一个 ​templates ​目录。Django 将会在这个目录里查找模板文件。
你项目的 ​TEMPLATES ​配置项描述了 Django 如何载入和渲染模板。默认的设置文件设置了 ​DjangoTemplates ​后端,并将 ​APP_DIRS ​设置成了 True。这一选项将会让 ​DjangoTemplates ​在每个 ​INSTALLED_APPS ​文件夹中寻找 “templates” 子目录。这就是为什么尽管我们没有像在第二部分中那样修改 ​DIRS ​设置,Django 也能正确找到 polls 的模板位置的原因。
在你刚刚创建的 ​templates目录里,再创建一个目录 ​polls​,然后在其中新建一个文件 ​index.html​ 。换句话说,你的模板文件的路径应该是 ​polls/templates/polls/index.html​ 。因为​app_directories​ 模板加载器是通过上述描述的方法运行的,所以 Django 可以引用到 ​polls/index.html​ 这一模板了。

将下面的代码输入到刚刚创建的模板文件中:

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

更新一下 ​polls/views.py​ 里的 ​index视图来使用模板:

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))

上述代码的作用是,载入 ​polls/index.html​ 模板文件,并且向它传递一个上下文(context)。这个上下文是一个字典,它将模板内的变量映射为 Python 对象。
用你的浏览器访问 “/polls/“ ,你将会看见一个无序列表。

Django4 - 第一个项目案例代码 第三节

栏目导航

  1. Django
  2. Pandas
  3. PyQt5

手机扫码访问