Django项目首页侧边栏内容调用接口处理
side.html
{% load static %}
<aside class="aside fr">
<div class="ad" style="margin-bottom: 20px;">
<a href="http://www.pyqt5.cn/vip/" rel="nofollow" target="_blank">
<img src="{% static 'image/banner3.jpg' %}" alt="PyQt5全套教程">
</a>
</div>
<!-- 标签 开始 -->
<div class="sidebar_item">
<div class="side_content">
<h3>热门标签</h3>
<ul class="label">
{% for tag in tags %}
<li><a href="/tag-{{ tag.tag_tail }}.html" title="{{ tag.tag_title }}">{{ tag.tag_short_title }}</a></li>
{% endfor %}
</ul>
</div>
</div>
<!-- 标签 结束 -->
<!-- 最新文章 开始 -->
<div class="sidebar_item">
<div class="side_content">
<h3>最新文章</h3>
<ul class="item_ul">
{% for new_art in new_arts %}
<li class="item_li">
<a href="/art-{{ new_art.art_tail }}.html">{{ new_art.art_title }}</a>
<span>{{ new_art.art_time }}</span>
</li>
{% endfor %}
</ul>
</div>
</div>
<!-- 最新文章 结束 -->
<!-- 热门文章 开始 -->
<div class="sidebar_item">
<div class="side_content">
<h3>热门文章</h3>
<ul class="hot_ul">
{% for hot_art in hot_arts %}
<li class="hot_li">
<span>{{ forloop.counter }}</span>
<a href="/art-{{ hot_art.art_title }}.html" title="{{ hot_art.art_title }}">{{ hot_art.art_title }}</a>
</li>
{% endfor %}
</ul>
</div>
</div>
<!-- 热门文章 结束 -->
<!-- 上图下文 开始 -->
<div class="sidebar_item">
<div class="side_content">
<h3>模板推荐</h3>
<ul class="img_ul">
</ul>
</div>
</div>
<!-- 上图下文 结束 -->
<!-- 侧栏广告图片下 -->
<div class="ad" style="margin-bottom: 20px;">
<a href="http://www.pyqt5.cn/vip/" rel="nofollow" target="_blank">
<img src="{% static 'image/banner1.jpg' %}" alt="PyQt5全套教程">
</a>
</div>
<!-- 侧栏广告图片下 -->
</aside>
<!-- 右侧边栏 -->
index.html
<!-- 右边侧边栏主体 开始 -->
{% include "side.html" %}
<!-- 右侧边栏 -->
views.py
def index(request):
if request.method == 'GET':
video_lists = Video_List.objects.all().order_by('video_list_order')
softs = Download.objects.all().order_by('down_time')[:3]
arts = Art.objects.all().order_by('art_time')[:10]
# 侧边栏
tags = Side.side_tags
new_arts = Side.side_new_arts
hot_arts = Side.side_hot_arts
context = {'video_lists': video_lists, 'softs': softs, 'arts': arts, 'tags': tags, 'new_arts': new_arts, 'hot_arts': hot_arts}
return render(request, 'index.html', context=context)