程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django (II) exquisite blog construction (10) realize the bug solution of the previous / next article on my page and article details page

編輯:Python

Preface

This chapter focuses on 【 About me 】 The specific implementation of the page , And what we left before 【 An article on the article details page / Next jump bug The solution of the 】

  • ps: that 【 About me 】 Interface words , It's very simple. , It's all dead data , Just configure the routing , Simply change the front-end copy yourself
    Here I mainly talk about 【 Last one / Next 】bug Solutions

Environmental Science :

  • Pycharm
  • python3.6
  • mysql 5.7
  • django 2.0.13


One 、 Overview of new feature items




Two 、 Last one / Next bug solve

1、article/views.py

  • Add logic directly to the original article details view function , I wrote comments in the code , You can look at the notes in detail 【 The first 3 spot 】

from django.shortcuts import render
# Create your views here.
from article.models import Article
"""
Article related view functions
"""
def article_detail(request):
"""
adopt id See article details
:param request:
:return:
"""
# Get the current article id
article_id = request.GET.get('id')
# print(" Current article id type :", type(article_id))
# according to id Get the current article
current_article = Article.objects.get(pk=article_id)
# 1、 The number of views is added synchronously : Click once , The number of views is synchronized with 1
current_article.click_num += 1
current_article.save()
# 2、 Query related articles : That is, the front in the corresponding label 6 Data
tags_list = current_article.tags.all() # First get the list of tags
# Definition 【 Related articles 】list
about_article_list = []
for tag in tags_list:
# Traverse to get the list of articles in the corresponding tag of the article
for about_article in tag.article_set.all():
# The article doesn't exist list Li and less than 6 piece , Is on the list in
if about_article not in about_article_list and len(about_article_list) < 6:
about_article_list.append(about_article)
# print("about_article_list:", about_article_list)
# 3、 Get the last one / The next article is for
all_article = Article.objects.all() # Get all the articles
print("all_article:", all_article)
previous_index = 0
next_index = 0
previous_article = None
next_article = None
# enumerate Make it into an index sequence , Use it to get both index and value 
for index, article in enumerate(all_article):
# When index by 0, That is, the current article is the first , The first article is not the last one , The default index is 0, The next one is index+1
if index == 0:
previous_index = 0
next_index = index + 1
# When index Is the total length -1 when , That is, the current article is the last , The last one is index-1, Next is the current article
elif index == len(all_article) - 1:
previous_index = index - 1
next_index = index
# otherwise , When index There is a previous article / In the next article , The last one is index-1, The next one is index+1
else:
previous_index = index - 1
next_index = index + 1
print(" Print the current article again id:", article_id)
# If traversal id Equal to the details of the current article id, There's a hole here , This article_id The original type is str, Need to change to int, Otherwise, it will report a mistake
if int(article_id) == article.id:
previous_article = all_article[previous_index]
next_article = all_article[next_index]
return render(request, 'article/info.html',
context={
'current_article': current_article, 'about_article_list': about_article_list,
'previous_article': previous_article, 'next_article': next_article})

2、templates/article/info.html

  • The page value can be changed slightly

{
% extends 'base.html' %}
{
% load staticfiles %}
{
% block title %}
Blog details
{
% endblock %}
{
# css Style part #}
{
% block mycss %}
{
# Introduce style info.css ,m.css #}
<link href="{% static 'css/info.css' %}" rel="stylesheet">
<link href="{% static 'css/m.css' %}" rel="stylesheet">
{
% endblock %}
{
# The content part #}
{
% block content %}
<div class="infos">
<div class="newsview">
{
# Embedded title #}
<h2 class="intitle"> Your present position is :<a href="{% url 'index' %}"> homepage </a> > <a
href="{% url 'article:detail' %}?id={
{ current_article.id }}"> Article details </a></h2>
<h3 class="news_title">{
{
 current_article.title }}</h3>
{
# author #}
<div class="news_author">
<span class="au01">{
{
 current_article.user.username }}</span>
<span class="au02">{
{
 current_article.date }}</span>
<span class="au03"> common <b>{
{
 current_article.click_num }}</b> People gathered around </span>
</div>
{
# label #}
<div class="tags">
{
# Traverse to get all the tags of the article #}
{
% for tag in current_article.tags.all %}
<a href="/">{
{
 tag.name }}</a>
{
% endfor %}
</div>
{
# brief introduction #}
<div class="news_about">
<strong > brief introduction :</strong>{
{
 current_article.description }}
</div>
{
# Article details #}
<div class="news_infos">
{
# Be careful , If xadmin Added in the background article p label , The content quoted here should add safe To filter , Otherwise, it will put p Labels are displayed directly on the page #}
{
{
 current_article.content |safe }}
<p>
</p>
</div>
</div>
</div>
{
# Last one / Next #}
<div class="nextinfo">
{
# Before, we used to transfer parameters directly from the front end +1,-1,bug Repaired #}
<p> Last one :<a href="{% url 'article:detail' %}?id={
{ previous_article.id }}">{
{
 previous_article.title }}</a>
</p>
<p> Next :<a href="{% url 'article:detail' %}?id={
{ next_article.id }}">{
{
 next_article.title }}</a></p>
</div>
{
# Related articles #}
<div class="otherlink">
<h2> Related articles </h2>
<ul class="otherlink_ul">
{
% for about_article in about_article_list %}
<li><a href="{% url 'article:detail' %}?id={
{ about_article.id }}">{
{
 about_article.title }}</a></li>
{
% endfor %}
</ul>
</div>
{
# Article review #}
{
# <div class="news_pl">#}
{
# <h2> Article review </h2>#}
{
# Article review 1#}
{
# Article review 2#}
{
# </div>#}
{
% endblock %}


3、 ... and 、 About my interface implementation

  • Because I am related to users , I will write the code uniformly in user It's working

1、urls.py

# About me
path('about', about_me, name='about'),

2、views.py

def about_me(request):
"""
return 【 About me 】 page
:param request:
:return:
"""
return render(request, 'user/about.html')

3、templates/user/about.html

  • user Next, add a new one about.html

{
% extends 'base.html' %}
{
% load staticfiles %}
{
# title part #}
{
% block title %}
Mikasa Personal blog - A personal blog site of a female programmer who loves to knock code
{
% endblock %}
{
# css Style part #}
{
% block mycss %}
<link href="{% static 'css/base.css' %}" rel="stylesheet">
<link href="{% static 'css/about.css' %}" rel="stylesheet">
{
% endblock %}
{
# The content part #}
{
% block content %}
<div class="container">
<div class="banner">
<p data-scroll-reveal="enter top over 2s"> I'm no longer patient with something , It's not because I became proud </p>
<p data-scroll-reveal="enter left over 2s after 1s"> It's just that my life has reached a stage , I don't want to waste any more time on something that makes me unhappy </p>
<p data-scroll-reveal="enter bottom over 2s after 2s"> I don't want to please people who don't like me , My love is only selfish for the people I love and the people who love me </p>
</div>
<div class="memorial_day">
<div class="time_axis"></div>
<ul>
<li class="n1"><a href="/">C++ introduction </a>
<div class="dateview">2018</div>
</li>
<li class="n2"><a href="/"> self-taught Java</a>
<div class="dateview">2019-2020</div>
</li>
<li class="n3"><a href="/"> Transition test </a>
<div class="dateview">2020-2021</div>
</li>
<li class="n4"><a href="/"> Switch to automation </a>
<div class="dateview">2021- so far </div>
</li>
<li class="n5"><a href="/"> A technology blogger </a>
<div class="dateview">2022</div>
</li>
</ul>
</div>
<div class="about left">
<h2>Just about me</h2>
<ul>
<p>
Mikasa, Woman , Simple record sharing
</p>
</ul>
<h2>About my blog</h2>
<p>CSDN Blog :<a href="https://blog.csdn.net/Makasa" target="_blank" class="blog_link">https://blog.csdn.net/Makasa</a>
</p>
</div>
</div>
</aside>
{
% endblock %}
{
# js #}
{
% block myjs %}
{
% endblock %}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved