1、页面继承
定义base.html:
<!DOCTYPE html>
<html>
{% load staticfiles %}
<head>
.....
<body>
{% block content %}
{% endblock %}
<div class="footer">
....
</div>
</body>
</html>
index.html:
{% extends 'base.html' %}
{% block title %}<title>在线学习系统--首页</title>{% endblock %}
{% load staticfiles %}
{% block content %}
........
{% endblock %}
2、分页、排序
分页插件:https://github.com/jamespacileo/django-pure-pagination#settings
settings.py:
INSTALLED_APPS = [
......
'pure_pagination', # 注册分页app
]
# 分页
PAGINATION_SETTINGS = {
'PAGE_RANGE_DISPLAYED': 10,
'MARGIN_PAGES_DISPLAYED': 2, 'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}
views.py:
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger
def video_list(request):
video_list = Video.objects.all()
video_page_count = video_list.count();
# 分页功能
try:
page = request.GET.get('page', 1) # 获取当前页码,如果没有默认1
except PageNotAnInteger: # 如果获取页码出错,默认1
page = 1 tmp = Paginator(video_list, 2, request=request) # 执行分页函数,参数1数据库的数据,参数2显示多少条数据,参数3request
video_page = tmp.page(page) # 返回一个,包含了分页数据和分页导航的对象
return render(request, "video_list.html", {"video_page": video_page,
"video_page_count": video_page_count})
html:
<div class="list-item-title">视频列表</div>
<div class="list-item-content clearfix">
{% for video in video_page.object_list %}
<div class="item">
<a href="">
<img src="{{ MEDIA_URL }}{{ video.image }}">
<p class="title">{{ video.name }}</p>
</a>
</div>
{% endfor %} </div>
{% load i18n %}
<div class="pagination">
{% if video_page.has_previous %}
<a href="?{{ video_page.previous_page_number.querystring }}"
class="prev">‹‹ {% trans "previous" %}</a>
{% else %}
<span class="disabled prev">‹‹ {% trans "previous" %}</span>
{% endif %}
{% for page in video_page.pages %}
{% if page %}
{% ifequal page video_page.number %}
<span class="current page">{{ page }}</span>
{% else %}
<a href="?{{ page.querystring }}" class="page">{{ page }}</a>
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if video_page.has_next %}
<a href="?{{ video_page.next_page_number.querystring }}" class="next">{% trans "next" %} ››</a>
{% else %}
<span class="disabled next">{% trans "next" %} ››</span>
{% endif %}
</div>
3、ModelForm提交、验证