python - Django 自定义标签过滤
问题描述
需要一点帮助。
我有一个新闻网站,我想做的是制作一个自定义标签(它必须是一个自定义标签,这是我的任务的一部分),它将显示一个类别中的最新 3 条新闻。因此,假设我单击“健康”文章,文章打开,我的自定义标签在文章旁边呈现来自健康类别的最新 3 条新闻。我希望我没有让你感到困惑,我还在学习。
我的标签:
from django import template
from news.models import Article
register = template.Library()
@register.inclusion_tag("news/categories.html")
def show_results(category=None):
article = Article.objects.all()
if category:
article = article.filter(category=category)
return {'article': article[:3]}
我的模型:
class Category(models.Model):
category_title = models.CharField(max_length=200, default="")
def __str__(self):
return self.category_title
class Article(models.Model):
title = models.CharField('title', max_length=200, blank=True)
slug = AutoSlugField(populate_from='title', default="",
always_update=True, unique=True)
author = models.CharField('Author', max_length=200, default="")
description = models.TextField('Description', default="")
is_published = models.BooleanField(default=False)
article_text = models.TextField('Article text', default="")
pub_date = models.DateTimeField(default=datetime.now, blank=True)
article_image = models.ImageField('Article Image')
article_category = models.ForeignKey(Category, on_delete="models.CASCADE", default="")
img2 = models.ImageField('Article Image 2', default="", blank=True)
img3 = models.ImageField('Article Image 3', default="", blank=True)
img4 = models.ImageField('Article Image 4', default="", blank=True)
img5 = models.ImageField('Article Image 5', default="", blank=True)
img6 = models.ImageField('Article Image 6', default="", blank=True)
def __str__(self):
return self.title
我的看法:
from django.shortcuts import render, reverse, get_object_or_404
from django.views import generic
from news.models import Article, Category
from .forms import CommentForm
from django.http import HttpResponseRedirect
class IndexView(generic.ListView):
template_name = 'news/index.html'
context_object_name = 'latest_article_list'
def get_queryset(self):
return Article.objects.order_by("-pub_date").filter(is_published=True)[:6]
class CategoryView(generic.ListView):
template_name = 'news/categories.html'
context_object_name = 'category'
def get_queryset(self):
return Article.objects.filter(article_category__category_title="Politics")
class ArticlesView(generic.ListView):
context_object_name = 'latest_article_list'
template_name = 'news/articles.html'
paginate_by = 5
def get_context_data(self, **kwargs):
context = super(ArticlesView, self).get_context_data(**kwargs)
context['categories'] = Category.objects.all()
return context
def get_queryset(self):
category_pk = self.request.GET.get('pk', None)
if category_pk:
return Article.objects.filter(article_category__pk=category_pk).order_by("-pub_date")
return Article.objects.order_by("-pub_date")
def article(request, article_id):
article = get_object_or_404(Article, pk=article_id)
context = {'article': article}
return render(request, 'news/article.html', context)
编辑:所以,为了进一步澄清,我的问题是:当我点击一篇“健康”文章并打开它时,我如何让我的自定义标签只过滤来自该特定类别的文章,抱歉,如果它令人困惑!所以如果我点击健康,我需要标签来呈现最新的 3 篇健康文章,如果我点击“音乐”文章,我需要标签来呈现来自“音乐”类别的最新 3 条新闻……我希望我没有让你更困惑!
非常感谢你!!!
编辑2:我的html:
{% extends "news-base.html" %}
{% load static %}
{% load article_extras %}
{% block article %}
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
{% show_results article_category %}
<div class="preloader d-flex align-items-center justify-content-center">
<div class="spinner">
<div class="double-bounce1"></div>
<div class="double-bounce2"></div>
</div>
</div>
<!-- {% show_results article.category %} -->
<!-- ##### Post Details Area Start ##### -->
<section class="container post-details-area">
<div class="container single-article-div">
<hr class="hr-single">
<h2 class="single-article-titles">{{ article.title }}</h2>
<hr class="hr-single">
<img class="single-article-img" src="{{ article.article_image.url }}" alt="">
<!-- *********************************** -->
<hr class="hr-single">
<p>Category: {{ article.article_category }}</p>
<hr class="hr-single">
<div class="row justify-content-center">
<!-- Post Details Content Area -->
<div class="col-12 col-xl-8">
<div class="post-details-content bg-white box-shadow">
<div class="blog-thumb">
</div>
<div class="blog-content">
<div class="post-meta">
<a href="#">{{ article.pub_date }}</a>
</div>
<h3 class="single-article-titles post-title"> {{ article.description }}</h3>
<hr>
<!-- Post Meta -->
<div class="post-meta-2">
<a href="#"><i class="fa fa-eye" aria-hidden="true"></i> 1034</a>
<a href="#"><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> 834</a>
<a href="#"><i class="fa fa-comments-o" aria-hidden="true"></i> 234</a>
</div>
<p>{{ article.article_text }}</p>
<hr />
{% include "partials/_thumbnails.html" %}
<hr>
<p>Author: {{ article.author }}</p>
<hr>
{% for comment in article.comments.all %}
<div class="comment">
<div class="date">{{ comment.created_date }}</div>
<strong>{{ comment.author }}</strong>
<p>{{ comment.text|linebreaks }}</p>
</div>
{% empty %}
<p>No comments here yet :(</p>
{% endfor %}
</div>
<!-- Post A Comment Area -->
<div class="post-a-comment-area bg-white mb-30 p-30 box-shadow clearfix">
<!-- Section Title -->
<div class="section-heading">
<h5>LEAVE A REPLY</h5>
</div>
<!-- Reply Form -->
<div class="contact-form-area">
<form action="#" method="post">
<div class="row">
<div class="col-12 col-lg-6">
<input type="text" class="form-control comment-section" id="name" placeholder="Your Name*"
required />
</div>
<div class="col-12 col-lg-6">
<input type="email" class="form-control comment-section" id="email" placeholder="Your Email*"
required />
</div>
<div class="col-12">
<textarea name="message" class="form-control" id="message" cols="30" rows="10"
placeholder="Message*" required></textarea>
</div>
<div class="col-12">
<button class="btn mag-btn comment-section" type="submit">
Submit Comment
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<script>
jQuery(document).ready(function ($) {
//set here the speed to change the slides in the carousel
$('#myCarousel').carousel({
interval: 5000
});
//Loads the html to each slider. Write in the "div id="slide-content-x" what you want to show in each slide
$('#carousel-text').html($('#slide-content-0').html());
//Handles the carousel thumbnails
$('[id^=carousel-selector-]').click(function () {
var id = this.id.substr(this.id.lastIndexOf("-") + 1);
var id = parseInt(id);
$('#myCarousel').carousel(id);
});
// When the carousel slides, auto update the text
$('#myCarousel').on('slid.bs.carousel', function (e) {
var id = $('.item.active').data('slide-number');
$('#carousel-text').html($('#slide-content-' + id).html());
});
});
</script>
{% endblock %}
解决方案
您的模板{% show_results article_category %}
使用该变量article_category
,但您的视图并未在上下文中公开此变量。将它包含在您的上下文中应该可以解决您的问题,即:
def article(request, article_id):
article = get_object_or_404(Article, pk=article_id)
context = {
'article': article,
'article_category': article.article_category.category_title
}
return render(request, 'news/article.html', context)
哦,刚刚注意到...,您的模板标签试图过滤category
:
if category:
article = article.filter(category=category)
但模型中字段的名称是article_category
. 此外,由于它是外键,因此您需要:
if category:
article = article.filter(article_category__category_title=category)
即按指向article_category 的category_title 字段过滤。
推荐阅读
- reactjs - 如何在两个组件上使用 withTheme HOC?
- javascript - Django 只能处理 ASGI/HTTP 连接,不能处理 websocket
- webpack - Webpack 开发服务器 - 在文件保存时将 CSS 文件合并到一个文件中
- java - 不包括 JUnit5 中 @BeforeEach 和 @AfterEach 的持续时间
- apache - baserewrite后如何防止目录访问
- java - Java - 为什么不允许泛型子类型化但允许数组子类型化
- javascript - 只有在 React 中返回 promise 后,我如何绘制图表?
- powershell - PowerShell:如何使用带有整个数据集值的 for 循环发送一封邮件
- python - 使用值作为键将数据框转换为字典
- c - 为什么当我交换这两行时程序没有填充数组