项目源码下载: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 %}
{% if article_list %}
{% for article in article_list %}
-
推荐
{{ article.content|striptags|safe|slice:"80" }}
{{ article.author.username }}
{{ article.publish_date |date:"Y-m-d H:i:s"}}
{{ article.item.title }}
{% endfor %}
{% else %}
{% endif %}
{% include 'page.html' %}
{% endblock %}
page.html
{% if num_pages > 1 %}
{% 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 %}
{{ article.title }}
{{ article.content| safe }}
{% 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 %}
标签{{ request.GET.tag }}
{% for article in article_list %}
{{ article.title }}
{% for tag in article.tag.all %}
{{ tag.name }}
{% endfor %}
{{ article.date_publish | date:'Y-m-d' }}
{% endfor %}
{% 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 %}
{% for item in items_list %}
{{ item.title }}
{% endfor %}
{% endif %}
{% if article_list %}
{% for article in article_list %}
-
推荐
{{ article.content|striptags|safe|slice:"80" }}
{{ article.author.username }}
{{ article.publish_date }}
{{ article.item.title }}
{% endfor %}
{% else %}
{% endif %}
{% include 'page.html' %}
{% endblock %}
最后添加urls.py文件中添加路径
path('item/', item, name='item'),
path('tag/', tag, name='tag'),