添加跳转方法
# 用模板显示响应的内容
from django.shortcuts import render_to_response, get_objects_or_404
from .models import Blog
# 博客列表
def blog_list(request):
content = {}
# 字典的key是对应的名称
content['blogs'] = Blog.objects.all()
# 传给前台数据并取得响应
return render_to_response('blog_list.html', content)
# 博客详情
def blog_detail(request, blog_pk):
content = {}
# 根据主键查询博客
content['blog'] = get_object_or_404(Blog, pk=blog_pk)
return render_to_response('blog_detail.html', content)
前端页面
blog_detail.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{ blog.title }}</title>
</head>
<body>
<h3>{{ blog.title }}</h3>
<p>{{ blog.content }}</p>
</html>
blog_list.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的网站</title>
</head>
<body>
{% for blog in blogs %}
<h3>{{ blog.title }}</h3>
<p>{{ blog.content }}</p>
{% endfor %}
</body>
</html>
访问某个博客url设置
blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('<int:blog_pk>', views.blog_detail, name='blog_detail'),
]
访问主页url设置
from django.contrib import admin
from django.urls import include, path
from blog.views import blog_list
urlpatterns = [
path('', blog_list, name='home'),
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
博客标题加上超链接
blog_list.py
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的网站</title>
</head>
<body>
<h2>个人博客网站</h2>
<hr>
{% for blog in blogs %}
<a href="{% url 'blog_detail' blog.pk %}">
<h3>{{ blog.title }}</h3>
</a>
<p>{{ blog.content }}</p>
{% endfor %}
</body>
</html>
添加博客总数的显示
第一种方法 : 添加{{ blogs | length }} , {{ blogs | length }}是一个过滤器
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的网站</title>
</head>
<body>
<h2>个人博客网站</h2>
<hr>
<h3>一共有{{ blogs | length }}篇博客</h3>
{% for blog in blogs %}
<a href="{% url 'blog_detail' blog.pk %}">
<h3>{{ blog.title }}</h3>
</a>
<p>{{ blog.content }}</p>
{% endfor %}
</body>
</html>
第二种方法 : 通过处理方法返回
views.py
# 博客列表
def blog_list(request):
content = {}
# 字典的key是对应的名称
content['blogs'] = Blog.objects.all()
content['count'] = Blog.objects.all().count()
# 传给前台数据并取得响应
return render_to_response('blog_list.html', content)
blog_list.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的网站</title>
</head>
<body>
<h2>个人博客网站</h2>
<hr>
<h3>一共有{{ count }}篇博客</h3>
{% for blog in blogs %}
<a href="{% url 'blog_detail' blog.pk %}">
<h3>{{ blog.title }}</h3>
</a>
<p>{{ blog.content }}</p>
{% endfor %}
</body>
</html>
判断为空,添加一条提示
使用{% empty %}, 在for标签里面可以拓展
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的网站</title>
</head>
<body>
<h2>个人博客网站</h2>
<hr>
<h3>一共有{{ count }}篇博客</h3>
{% for blog in blogs %}
<a href="{% url 'blog_detail' blog.pk %}">
<h3>{{ blog.title }}</h3>
</a>
<p>{{ blog.content }}</p>
{% empty %}
<p>-- 暂无博客, 敬请期待! --</p>
{% endfor %}
</body>
</html>
长博客显示限制
使用过滤器, 在要过滤的字段后面加上 | 和条件
<!DOCTYPE html>
<html>
...
<body>
<h2>个人博客网站</h2>
<hr>
{% for blog in blogs %}
<a href="{% url 'blog_detail' blog.pk %}">
<h3>{{ blog.title }}</h3>
</a>
<p>{{ blog.content | truncatechars:30 }}</p>
{% empty %}
<p>-- 暂无博客, 敬请期待! --</p>
{% endfor %}
<h3>一共有{{ count }}篇博客</h3>
</body>
</html>
过滤时间,添加显示字段
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{ blog.title }}</title>
</head>
<body>
<a href="{% url 'home' %}">
<h2>个人博客网站</h2>
</a>
<h3>{{ blog.title }}</h3>
<p>作者: {{ blog.author }}</p>
<p>发表时间: {{ blog.created_time | date:"Y-m-d H:n:s" }}</p>
<p>分类: {{ blog.blog_type }}</p>
<p>{{ blog.content }}</p>
</html>
博客类跳转到标签的功能
规定一个跳转的网址
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('<int:blog_pk>', views.blog_detail, name='blog_detail'),
path('<break/int:blog_type_pk>', views.blog_with_type, name='blog_with_type'),
]
处理方法
view.py
# 博客分类
def blog_with_type(request, blog_type_pk):
content = {}
blog_type = get_object_or_404(BlogType, pk=blog_type_pk)
content['blogs'] = Blog.objects.filter(blog_type=blog_type)
content['blog_type'] = blog_type
return render_to_response('blog_with_type.html', content)
模板页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{ blog_type.type_name }}</title>
</head>
<body>
<h2>个人博客网站</h2>
<hr>
<h2>{{ blog_type.type_name }}</h2>
{% for blog in blogs %}
<a href="{% url 'blog_detail' blog.pk %}">
<h3>{{ blog.title }}</h3>
</a>
<p>{{ blog.content | truncatechars:30 }}</p>
{% empty %}
<p>-- 暂无博客, 敬请期待! --</p>
{% endfor %}
<h3>一共有{{ blogs | length }}篇博客</h3>
</body>
</html>
常用的模板标签
循环 : for
条件 : if (可逻辑判断) , ifequal, ifnotequal
链接 : url
模板嵌套 : blogk, extends, include
注释 : {# #}