项目源码下载:https://github.com/Cherish-sun/NEWS/tree/master
一、新闻搜索功能
# 搜索
def search(request):
strquery = request.GET.get('query')
page = int(request.GET.get('page', 1))
data = {
"search": strquery,
"page": page,
"ordering": '-id',
}
article_data = getdata(articles_url, data)
article_list = article_data["results"]
# 总记录数
count = article_data["count"]
# 下一页
next = article_data["next"]
nextpage = page + 1
# 上一页
previous = article_data["previous"]
previouspage = page - 1
# 总页数
num_pages = int(count / PAGESIZE)
curr_url = request.get_full_path()
nPos = curr_url.find('&page')
if nPos > 0:
curr_url = request.get_full_path()[0:nPos]
else:
curr_url = request.get_full_path()
return render(request, 'searchlist.html', locals())
根据传入的字符串查询,并对查询结果分页传给searchlist.html
{% extends 'base.html' %}
{% load staticfiles %}
{% block left_content %}
<div class="article_div module">
<ul id="new_div">
{% if article_list %}
{% for article in article_list %}
<li class="news">
<div>
<div class="flag">推荐</div>
<a href="{% url 'article' %}?id={{ article.id }}" target="_blank">
<img src="{{ article.pic }} " class="pic"></a>
<div class="detail">
<div class="dtitle">
<a href="{% url 'article' %}?id={{ article.id }}" target="_blank"><h2>{{ article.id }}-{{ article.title }}</h2></a>
</div>
<div class="desc">{{ article.content|striptags|safe|slice:"80" }}</div>
<div class="info">
<img src="/static/images/neter.jpg" class="img_small"><span>{{ article.author.username }}</span>
<div class="pv">
<span class="push-time">{{ article.publish_date |date:"Y-m-d H:i:s"}}</span><span class="column">
<a href="/">{{ article.item.title }}</a> </span>
</div>
</div>
</div>
</div>
</li>
{% endfor %}
{% else %}
<div><img style="width:200px;padding-left:280px; " src="/static/images/nodata1.png"></div>
{% endif %}
</ul>
</div>
{% include 'page.html' %}
{% endblock %}
page.html
{% if num_pages > 1 %}
<div class="more">
<span class="step-links">
{% if previous %}
<a href="{{ curr_url }}&page={{previouspage}}">上一页</a>
{% endif %}
<span class="current">
Page {{ page }} of {{ num_pages }}.
</span>
<span class="current">
{% if next %}
<a href="{{ curr_url }}&page={{nextpage}}" class="nextpage">下一页</a>
<a class="nextpage2"></a>
{% endif %}
</span>
</div>
{% endif %}
二、文章详情页
# 文章详情页
def article(request):
id = request.GET.get('id')
articles_url = 'http://127.0.0.1:8005/articleList/'
# 构造url http://127.0.0.1:8000/article/id/
articles_url = urljoin(articles_url, id)
article = getdata(articles_url)
return render(request, 'article.html', locals())
article.html
{% extends 'base.html' %}
{% load staticfiles %}
{% block left_content %}
<div class="articlecss">
<div class="title">
<h2>{{ article.title }}</h2>
<div class="postmeta">
<span class="postmeta_author">{{ article.author.username }}</span>
<span class="postmeta_category"><a href="" rel="category">{{ article.item.title }}</a></span>
<span class="a_ilike">浏览数{{ article.read_num }}</span>
<span class="a_ilike">
<a id = 'articlefav' href="javascript:void(0)">收藏</a>
</span>
<span class="postmeta_time">{{ article.publish_date | date:'Y-m-d' }}</span>
</div>
</div>
<div class="article">
<p> {{ article.content| safe }}</p>
</div>
<span class="tags">
{% for tag in article.tags %}
<a href="{% url 'tag' %}?tagid={{ tag.id }}" rel="tag">{{ tag.name }} </a>
{% endfor %}
</span>
</div>
<script >
//收藏
$('#articlefav').click(function(){
userid = $.cookie('user_id');
var isfav = $('#articlefav').html();
if (isfav == '已收藏'){
return;
}
if (userid == null){
$('.cd-user-modal').addClass('is-visible');
$('#cd-login').addClass('is-selected');
$('.cd-switcher').children('li').eq(0).children('a').addClass('selected');
return;
}
var var_data = {
"articles": {{ article.id }},
"user": userid,
"token ": $.cookie('api_token'),
}
var json_str = JSON.stringify(var_data);
$.ajax({
url: "http://127.0.0.1:8005/userFav/",
type: "POST",
data: json_str,
contentType: "application/json",
dataType: "json",
success: function (data) {
setTimeout(function(){
}, 1000);
//alert(data);
if (data) {
$('#articlefav').html('已收藏');
$form_modal.removeClass('is-visible');
}
else {
// alert(data.msg);
signup_selected();
ErrorInfo.removeClass().addClass("inputerror").html(data.msg);
$('#register-form input[id="email"]').focus();
return false;
}
},
error:function(XMLHttpRequest, textStatus, errorThrown){
//alert(JSON.stringify(XMLHttpRequest) + "\n" + textStatus + "\n" + errorThrown);
//alert(errorThrown);
}
})
return false;
})
</script>
{% endblock %}
三、按标签查询对应的文章列表
def tag(request):
tagid = request.GET.get('tagid')
page = int(request.GET.get('page', 1))
data = {
"tags": tagid,
"page": page,
"ordering": '-id',
}
article_data = getdata(articles_url, data)
article_list = article_data["results"]
# 总记录数
count = article_data["count"]
# 下一页
next = article_data["next"]
nextpage = page + 1
# 上一页
previous = article_data["previous"]
previouspage = page - 1
# 总页数
num_pages = int(count / PAGESIZE)
curr_url = request.get_full_path()
nPos = curr_url.find('&page')
if nPos > 0:
curr_url = request.get_full_path()[0:nPos]
else:
curr_url = request.get_full_path()
return render(request, 'tag.html', locals())
tag.html
{% extends 'base.html' %}
{% load staticfiles %}
{% block left_content %}
<div class="topnews">
<h2>标签{{ request.GET.tag }}</h2>
{% for article in article_list %}
<div class="blogs">
<ul>
<h3><a href="{% url 'article' %}?id={{ article.id }}">{{ article.title }}</a></h3>
<p><a href="{% url 'article' %}?id={{ article.id }}">{{ article.desc }}</a></p>
<p class="autor"><span class="lm f_l">
{% for tag in article.tag.all %}
<a href="/">{{ tag.name }}</a> </span>
{% endfor %}
<span class="dtime f_l">{{ article.date_publish | date:'Y-m-d' }}</span>
</ul>
</div>
{% endfor %}
</div>
{% endblock %}
四、分类页
def category(request):
categoryid = request.GET.get('cid')
print(categoryid)
currcategoryid = request.GET.get('cid')
print(currcategoryid)
page = int(request.GET.get('page', 1))
# 取二级栏目
data = {
"categorys": categoryid,
}
ul = "http://127.0.0.1:8005/categoryitems/"
print(ul)
print(data)
items_list = getdata(ul, data)
print(99, items_list)
# for i in items_li:
# items_list = (i["items"])
# 取新闻
data = {
"item__categorys": categoryid,
"page": page,
"ordering": '-id',
}
article_data = getdata(articles_url, data)
article_list = article_data["results"]
# 总记录数
count = article_data["count"]
# 下一页
next = article_data["next"]
nextpage = page + 1
# 上一页
previous = article_data["previous"]
previouspage = page - 1
# 总页数
num_pages = int(count / PAGESIZE)
curr_url = request.get_full_path()
nPos = curr_url.find('&page')
if nPos > 0:
curr_url = request.get_full_path()[0:nPos]
else:
curr_url = request.get_full_path()
return render(request, 'category.html', locals())
五、根据二级栏目类型取新闻列表
def item(request):
categoryid = request.GET.get('cid')
itemid = request.GET.get('itemid')
page = int(request.GET.get('page', 1))
# 取二级栏目
data = {
"categorys": categoryid,
}
items_data = getdata(items_url, data)
items_list = items_data
# 取新闻
data = {
"item": itemid,
"page": page,
"ordering": '-id',
}
article_data = getdata(articles_url, data)
article_list = article_data["results"]
# 总记录数
count = article_data["count"]
# 下一页
next = article_data["next"]
nextpage = page + 1
# 上一页
previous = article_data["previous"]
previouspage = page - 1
# 总页数
num_pages = int(count / PAGESIZE)
curr_url = request.get_full_path()
nPos = curr_url.find('&page')
if nPos > 0:
curr_url = request.get_full_path()[0:nPos]
else:
curr_url = request.get_full_path()
return render(request, 'category.html', locals())
category.html
{% extends 'base.html' %}
{% load staticfiles %}
{% block left_content %}
{% if items_list %}
<div class="notes clearfix" id="authors">
{% for item in items_list %}
<li class="clearfix">
<a href="{% url 'item' %}?cid={{ item.categorys.id }}&itemid={{ item.id }}">
{{ item.title }}
</a>
</li>
{% endfor %}
</div>
{% endif %}
<div class="article module">
<ul id="new_div">
{% if article_list %}
{% for article in article_list %}
<li class="news">
<div>
<div class="flag">推荐</div>
<a href="{% url 'article' %}?id={{ article.id }}" target="_blank"><div class="layer"></div>
<img src="{{ article.pic }} " class="pic"></a>
<div class="detail">
<div class="dtitle">
<a href="{% url 'article' %}?id={{ article.id }}" target="_blank"><h2>{{ article.id }}-{{ article.title }}</h2></a>
</div>
<div class="desc">{{ article.content|striptags|safe|slice:"80" }}</div>
<div class="info">
<img src="/static/images/avatar3.png" class="img_small"><span>{{ article.author.username }}</span></a>
<div class="pv">
<span class="push-time">{{ article.publish_date }}</span>
<span class="column"> <a href="/">{{ article.item.title }}</a> </span>
</div>
</div>
</div>
</div>
</li>
{% endfor %}
{% else %}
<div class="nodata"><img style="width:200px;padding-left:280px; " src="/static/images/nodata1.png"></div>
{% endif %}
</ul>
{% include 'page.html' %}
</div>
{% endblock %}
最后添加urls.py文件中添加路径
path('item/', item, name='item'),
path('tag/', tag, name='tag'),