Djiango基础实例(3)

视图:一类具有相同功能和模板的网页的集合

投票应用中,需要下列几个视图:

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

在Djiango中,网页和其他内容都是从视图派生出来的。每一个视图表现为一个Python函数(或者说是方法,如果说是在基于类的视图里的话)

1.编写更多视图

在 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"),
]

view.py里,方法的参数question_id来源于url里int:question_id。
使用尖括号 “获得” 网址部分后发送给视图函数作为一个关键字参数。
int:question_id中的question_id定义了要使用的名字,用来识别相匹配的模式。
int 部分是一种转换形式,用来确定应该匹配网址路径的什么模式。
冒号 :用来分隔转换形式和模式名。

启动服务器,手动输入网址,进行一些简单的验证,看看网页显示的内容与views视图里函数的关系
例子:
http://127.0.0.1:8000/polls/2/
http://127.0.0.1:8000/polls/23/vote
http://127.0.0.1:8000/polls/12/result

2.写一个真正有用的视图

每个视图必须要做的只有两件事:

  • 返回一个包含被请求页面内容的 HttpResponse 对象
  • 或者抛出一个异常,比如 Http404

然后可以做你想做的任何事情

我们在 index() 函数里插入了一些新内容,让它能展示数据库里以发布日期排序的最近 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

问题:页面的设计写死在视图函数的代码里了,不方便修改。所以需要使用Django的模板系统,将页面的设置从代码中分离出来。

  • 首先需要在polls目录里创建一个templates目录,Django将会在这个目录里查找模板文件
  • settings.py中的TEMPALTES描述了Django如何载入和渲染模板

默认的设置文件设置了 DjangoTemplates 后端,并将 APP_DIRS 设置成了 True。这一选项将会让 DjangoTemplates 在每个 INSTALLED_APPS 文件夹中寻找 “templates” 子目录

  • 在你刚刚创建的 templates 目录里,再创建一个目录 polls,然后在其中新建一个文件 index.html 。模板文件的路径应该是 polls/templates/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 %}

注:在你自己创建的项目中,你应该使用 完整的 HTML 文档 。

更新一下 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/” ,你将会看见一个无序列表。列出了我们添加的 “What’s up” 投票问题,链接指向这个投票的详情页。

3.一个快捷函数render()

载入模板,填充上下文,再返回由它生成的 HttpResponse 对象」是一个非常常用的操作流程。于是 Django 提供了一个快捷函数,我们用它来重写 index() 视图

from django.shortcuts import render

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)

注意到,我们不再需要导入 loader 和 HttpResponse 。不过如果你还有其他函数(比如说 detail, results, 和 vote )需要用到它的话,就需要保持 HttpResponse 的导入。

抛出404问题

我们来处理投票详情视图——它会显示指定投票的问题标题。下面是这个视图的代码:

from django.http import Http404
from django.shortcuts import render

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() 函数获取一个对象,如果不存在就抛出 Http404 错误也是一个普遍的流程。Django 也提供了一个快捷函数,下面是修改后的详情 detail() 视图代码:

from django.shortcuts import get_object_or_404, render

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

使用模板系统

detail() 视图。它向模板传递了上下文变量 question 。下面是 polls/detail.html 模板里正式的代码:

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

模板系统统一使用点符号来访问变量的属性。

去除模板中的硬编码

在 polls/index.html 里编写投票链接时,链接是硬编码的

<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>

对于一个包含很多应用的项目来说,修改起来是十分困难的。因为你在 polls.urls 的 url() 函数中通过 name 参数为 URL 定义了名字,你可以使用 {% url %} 标签代替它:

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

可以在url文件中查看当时的定义
如果想改变投票详情中的url,只需要在polls/urls.py里稍微修改一下就可以了
例如:

...
# added the word 'specifics'
path("specifics/<int:question_id>/", views.detail, name="detail"),
...

为URL添加命名空间

在一个真实的项目中,可能会有十个,二十个甚至更多个应用,Django如何分辨重名的url,例如在polls应用中有detai视图,另一个博客应用也有同名视图,Django如何分辨?
答案:在根 URLconf 中添加命名空间。在 polls/urls.py 文件中稍作修改,加上 app_name 设置命名空间

from django.urls import path

from . import views

app_name = "polls"
urlpatterns = [
    path("", views.index, name="index"),
    path("<int:question_id>/", views.detail, name="detail"),
    path("<int:question_id>/results/", views.results, name="results"),
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

编辑 polls/index.html 文件,从

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

修改为指向具有命名空间的详细视图:

<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值