6 功能4:文章详情页

1、文章详情页的设计

    # 文章详情页
    re_path(r‘^(?P<username>\w+)/articles/(?P<article_id>\d+)$‘, views.article_detail, name=‘article_detail‘),

2、文章详情页的数据构建

def article_detail(request, username, article_id):
    """文章详情页"""

    user_obj = models.UserInfo.objects.filter(username=username).first()
    blog = user_obj.blog        # 查询当前站点对象
    article_list = models.Article.objects.filter(user=user_obj)
    cate_list = models.Category.objects.filter(blog=blog).values(‘pk‘).annotate(c=Count("article__title")).values_list("title", ‘c‘)
    tag_list = models.Tag.objects.filter(blog=blog).values(‘pk‘).annotate(c=Count(‘article‘)).values_list(‘title‘,‘c‘)
    date_list = models.Article.objects.filter(user=user_obj).extra(select={‘y_m_date‘:"date_format(create_time,‘%%Y-%%m‘)"}).values(‘y_m_date‘).annotate(c=Count(‘nid‘)).values_list(‘y_m_date‘, ‘c‘)

    return render(request,‘blog/article_detail.html‘,{"username":username,‘blog‘:blog,‘article_list‘:article_list,"tag_list":tag_list,"date_list":date_list, "cate_list":cate_list})

1、解决复用问题:方式1:封装函数

3、解决复用问题:方式2:inclution_tag  自定义tag

1、自定义的simple_tag

2.inclution_tag

# -*- coding: utf-8 -*-
# @Time    : 2018/08/04 0004 22:03
# @Author  : Venicid

from django import template
from django.db.models import Count
from blog import models

register = template.Library()

@register.simple_tag
def multi_tag(x,y):

    return x*y

@register.inclusion_tag("blog/classification.html")
def get_classification_style(username):

    user_obj = models.UserInfo.objects.filter(username=username).first()
    blog = user_obj.blog
    cate_list = models.Category.objects.filter(blog=blog).values(‘pk‘).annotate(c=Count("article__title")).values_list("title", ‘c‘)
    tag_list = models.Tag.objects.filter(blog=blog).values(‘pk‘).annotate(c=Count(‘article‘)).values_list(‘title‘,‘c‘)
    date_list = models.Article.objects.filter(user=user_obj).extra(select={‘y_m_date‘:"date_format(create_time,‘%%Y-%%m‘)"}).values(‘y_m_date‘).annotate(c=Count(‘nid‘)).values_list(‘y_m_date‘, ‘c‘)

    return {‘blog‘:blog,"tag_list":tag_list,"date_list":date_list, "cate_list":cate_list}

4、标签字符串转义

def article_detail(request, username, article_id):
    """文章详情页"""
    user_obj = models.UserInfo.objects.filter(username=username).first()
    blog = user_obj.blog
    # article_id对应的文章
    article_obj = models.Article.objects.filter(pk=article_id).first()

    return render(request,‘blog/article_detail.html‘,locals())

1、字符串转义

2、防止xss攻击

4

5

6

7

8

9

  

原文地址:https://www.cnblogs.com/venicid/p/9420513.html

时间: 2024-08-30 04:33:19

6 功能4:文章详情页的相关文章

Django 项目试炼blog(6) -- 文章详情页1 -- 点赞功能

url #文章详情页 re_path(r'(?P<username>\w+)/article/(?P<article_id>\d+)/$',views.article), # 点赞 path('up_down/', views.up_down), views from django.db.models import F def up_down(request): sign = request.POST.get('sign') sign = json.loads(sign) # 前端

博客2.0--第一版文章详情页留念

新版文章详情页>>http://blog.51cto.com/itstyle/2127815 原文地址:http://blog.51cto.com/51ctoblog/2129401

文章详情页

一.文章详情页访问设计 访问文章详情页,访问文章路径类似:https://www.cnblogs.com/wupeiqi/articles/3148888.html 参照访问路径编写文章详情页路由如下: urlpatterns = [ ... # 文章详情页 re_path('^(?P<username>\w+)/articles/(?P<article_id>\d+)$', views.article_detail), ] 二.文章详情页的数据构建 文章详情页的head部分和左侧

BBS - 文章详情页

一. 文章详情页的设计和数据构建 url.py # 文章详情页 re_path('^(?P<username>\w+)/articles/(?P<article_id>\d+)$', views.article_detail), # \w+:数字和字母 views.py def get_query_data(username): # 查询相关的数据,获取分类的数据 ''' 由于数据很多个地方需要用到,所以可以解耦出来,单独写一个函数 :param username: :return

文章详情页文章评论功能

一.文章评论功能实现流程 文章评论包含两种评论,根评论:对文章的评论:子评论:对评论的评论.两者的区别在于是否存在父评论. 实现流程:1.构建样式:2.提交根评论:3.显示根评论(分为render显示和Ajax显示):4.提交子评论:5.显示子评论(分为render显示和Ajax显示):6.评论树显示(博客园是楼层显示). 二.构建评论样式 1.article_detail.html: {# 文章点赞,清除浮动 #} <div class="clearfix"> <d

如何在ecshop文章详情页增加文章描述

1.准备文件:前台article.php 后台 admin/article.php   和   admin/templates/article_info.htm2.数据库: 执行下面语句(如有需要,则更改表头ecs)ALTER  TABLE  `esc_article`  ADD  `article_desc` LONGTEXT NOT  NULL  AFTER  `keywords`也可到phpMyAdmin 中 找到表 ecs_article, 添加字段article_desc, 属性设置如

Django 项目试炼blog(7) -- 文章详情页2 -- 前端样式的继承与楼评论显示

views from django.db import transaction def comment(request): article_id = request.POST.get('article_id') content = request.POST.get('content') pid = request.POST.get('parent_id') #事务操作 生成评论,文章数据中评论总数自动+1(同时执行) with transaction.atomic(): comm_obj = C

ecshop不同文章分类 不同文章详情页模板

修改article.php 1 <?php 2 if(isset($article) && $article['cat_id'] > 2 && $article['cat_id'] <>13) 3 { 4 $smarty->display('article.dwt', $cache_id); 5 } 6 elseif($article['cat_id'] == 13) 7 { 8 $smarty->display('article13.d

Django学习之实现文章详情页面的跳转

1.由于不支持博客首页到文章详情页的跳转,只能打开第一篇文章的详情页 2.所以需要做以下工作: 设计文章详情页的url,完善视图函数逻辑,实现首页跳转 /blog/detail =>不能指定某一篇文章 /blog/detail/1 => 博客唯一id唯1的文章 /blog/detail/2 => 博客唯一id唯2的文章 /blog/detail/3 => 博客唯一id唯3的文章 /blog/detail/...... 获取URL路径参数:<> 页面跳转:href指定ht