欢迎关注我的公众号:zx94_11
生成列表和视图
- 博客列表
- 博客详情
导包
from django.shortcuts import render, get_object_or_404
from .models import Post
博客列表
def post_list(request):
posts = Post.published.all()
return render(request,
'blog/post/list.html',
{'posts': posts})
博客详情
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post,
slug=post,
status='published',
publish__year=year,
publish__month=month,
publish__day=day)
return render(request,
'blog/post/detail.html',
{'post': post})
添加URL路径
打开blog/urls.py
写入
from django.urls import path
from . import views
app_name = 'blog'
urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int:day>/<slug:post>/',
views.post_detail, name='post_detail')
]
在mysite/urls.py
加上:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls', namespace='blog')),
]
模型的标准URL
由于post_detail的URL比较特别,所以在blog/models.py
中添加特殊的处理
from django.urls import reverse
class Post(models.Model):
...
def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.publish.year,
self.publish.month,
self.publish.day,
self.slug])
视图模版
base.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% static 'css/blog.css' %}">
</head>
<body>
<div id="content">
{% block content %}
{% endblock %}
</div>
<div id="sidebar">
<h2>My blog</h2>
<p>This is my blog.</p>
</div>
</body>
</html>
detail.html
{% extends 'blog/base.html' %}
{% block title %}
{{ post.title }}
{% endblock %}
{% block content %}
<h1>{{ post.title }}</h1>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{{ post.body|linebreaks }}
{% endblock %}
list.html
{% extends 'blog/base.html' %}
{% block title %}
My blog
{% endblock %}
{% block content %}
<h1>My Blog</h1>
{% for post in posts %}
<h2>
<a href="{{ post.get_absolute_url }}">
{{ post.title }}
</a>
</h2>
<p class="date">
Published {{ post.publish }} by {{ post.author }}
</p>
{#truncatewords:将数值截取至指定的单词数量#}
{#linebreaks:将输出结果转换为HTML换行符#}
{{ post.body|truncatewords:30|linebreaks }}
{% endfor %}
{% endblock %}
blog.css
body {
margin: 0;
padding: 0;
font-family: helvetica, sans-serif;
}
a {
color: #00abff;
text-decoration: none;
}
h1 {
font-weight: normal;
border-bottom: 1px solid #bbb;
padding: 0 0 10px 0;
}
h2 {
font-weight: normal;
margin: 30px 0 0;
}
#content {
float: left;
width: 60%;
padding: 0 0 0 30px;
}
#sidebar {
float: right;
width: 30%;
padding: 10px;
background: #efefef;
height: 100%;
}
p.date {
color: #ccc;
font-family: georgia, serif;
font-size: 12px;
font-style: italic;
}
.paginator {
margin: 40px 0;
font-weight: bold;
}
label {
float: left;
clear: both;
color: #333;
margin-bottom: 4px;
}
input, textarea {
clear: both;
float: left;
margin: 0 0 10px;
background: #ededed;
border: 0;
padding: 6px 10px;
font-size: 12px;
}
input[type=submit] {
font-weight: bold;
background: #00abff;
color: #fff;
padding: 10px 20px;
font-size: 14px;
text-transform: uppercase;
}
.errorlist {
color: #cc0033;
float: left;
clear: both;
padding-left: 10px;
}
.comment {
padding: 10px;
}
.comment:nth-child(even) {
background: #efefef;
}
.comment .info {
font-weight: bold;
font-size: 12px;
color: #666;
}
结果展示
添加分页机制
修改blog/views.py
的post_list
paginator = Paginator(object_list, 1)
里面的数字决定一页里面有几条数据,为了展示效果,先改为1
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def post_list(request):
object_list = Post.published.all()
paginator = Paginator(object_list, 1)
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# 如果「不是数字」则去第一页
posts = paginator.page(1)
except EmptyPage:
# 如果「页数太大」就去最后一页
posts = paginator.page(paginator.num_pages)
return render(request,
'blog/post/list.html',
{'posts': posts, 'page': page})
增加一个blog/templates/pagination.html
<div class="paginator">
<span class="step-links">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_num }}">Previous</a>
{% endif %}
<span class="current">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</div>
在blog/templates/blog/post/list.html
最后面添加
...
{% include "pagination.html" with page=posts %}
{% endblock %}