07_常用的模版标签和过滤器

常用的模版标签和过滤器

1、继续搭建blog

前面写了后台模型,这次我们来将其显示到前端页面。

①要有个博客的列表

②点进去就可以打开具体的博客页面

定义views.py

from django.shortcuts import render_to_response,get_object_or_404
from .models import Blog

# Create your views here.

def blog_list(request):
    context = {}
    context['blog'] = Blog.objects.all()
    return render_to_response('blog_list.html',context)

def blog_detail(request,blog_pk):
    context = {}
    context['blog'] = get_object_or_404(Blog,pk=blog_pk)
    return render_to_response('blog_detail.html',context)

接着,在前端页面blog_list.html里面把博客列表遍历出来

<div>
    <h3>个人博客网站</h3>
</div>
<body>
    {% for blog in blogs %}
        <h3>{{ blog.title }}</h3>
        <p>{{ blog.content }}</p>
    {% endfor %}
</body>

在详情页面blog_detail中

<body>
    <h3>{{ blog.title }}</h3>
    <p>{{ blog.content }}</p>
</body>

设置总路由和分路由

from django.contrib import admin
from django.urls import path,include
from blog.views import blog_list

urlpatterns = [
    path('',blog_list,name='home'),
    path('admin/', admin.site.urls),
    path('blog/',include('blog.urls')),
]
from django.urls import path
from . import views

# start with blog
urlpatterns = [
    # http://localhost:8000/blog/1
    path('<int:blog_pk>',views.blog_detail,name='blog_detail'),
]

查看一下网页

修改前端页面,使其能够互相跳转

blog_list.html

<div>
    <h3>个人博客网站</h3>
</div>
<hr>
{% for blog in blogs %}
    <a href="{% url 'blog_detail' blog.pk %}">
        <h3>{{ blog.title }}</h3>
    </a>
    <p>{{ blog.content }}</p>
{% endfor %}

blog_detail.html

<body>
    <div>
        <a href="{% url 'home' %}">
           <h3>个人博客网站</h3>
        </a>
    </div>
    <h3>{{ blog.title }}</h3>
    <p>{{ blog.content }}</p>
</body>

那么我们可以不可以做个东西,可以一目了然的看到有多少条博客?

①在blog_list中

<p>一共有{{ blogs|length }}篇博客</p>

②在views.py中,再写进前端页面{{ blogs_count }}

context['blogs_count'] = Blog.objects.all().count()

当博客数量为空时

{% for blog in blogs %}
    <a href="{% url 'blog_detail' blog.pk %}">
        <h3>{{ blog.title }}</h3>
    </a>
    <p>{{ blog.content }}</p>
{% empty %}
    <p>-- 暂无博客,敬请期待 --</p>
{% endfor %}
<p>一共有{{ blogs|length }}篇博客</p>

当我们博客内容很长时,全部显示在网页上不是很美观,所以我们要采用过滤器(比如只让它显示前30个字)

<p>{{ blog.content|truncatechars_html:30 }}</p>

来到详情页面blog_detail.html

<h3>{{ blog.title }}</h3>
<p>作者:{{ blog.author }}</p>
<p>发表日期:{{ blog.create_time }}</p>
<p>{{ blog.content }}</p>

我们只想要它的年月日,要怎么做呢?

使用过滤器:

<p>发表日期:{{ blog.create_time|date:"Y-m-d G:n:s" }}</p>

我们希望点击分类,可以查看到此类型的所有文章

新建blogs_with_type.html

<body>
    <div>
        <a href="{% url 'home' %}">
           <h3>个人博客网站</h3>
        </a>
    </div>
    <hr>
    <h3>{{ blog_type.type_name }}</h3>
    {% for blog in blogs %}
        <a href="{% url 'blog_detail' blog.pk %}">
            <h3>{{ blog.title }}</h3>
        </a>
        <p>{{ blog.content|truncatechars_html:30 }}</p>
    {% empty %}
        <p>-- 暂无博客,敬请期待 --</p>
    {% endfor %}
    <p>一共有{{ blogs|length }}篇博客</p>
</body>

在views.py中

def blogs_with_type(request,blog_type_pk):
    context = {}
    blog_type = get_object_or_404(BlogType,pk=blog_type_pk)
    context['blogs'] = Blog.objects.filter(blog_type=blog_type)
    context['blog_type'] = blog_type
    return render_to_response('blogs_with_type.html',context)

配置路由

path('type/<int:blog_type_pk>',views.blogs_with_type,name="blogs_with_type")

新增数据

2、常用的模板标签

3、常用过滤器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值