希望能得到一些帮助理解,每当我尝试按类别获取博客文章时,我都会收到以下错误: /blog/cat1 'NoneType' 对象的 AttributeError 没有属性 'views' “cat1”是我在 django 管理面板中创建用于测试的类别名称
这是我的 models.py 文件
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
from ckeditor_uploader.fields import RichTextUploadingField
# Create your models here.
class Post(models.Model):
sno = models.AutoField(primary_key=True)
title = models.CharField(max_length=255)
content = RichTextUploadingField()
thumbnail = models.ImageField(upload_to='featured_image/%Y/%m/%d/')
categories = models.ManyToManyField('Category', related_name='posts')
author = models.CharField(max_length=15)
slug = models.CharField(max_length=255)
views = models.IntegerField(default=0)
timeStamp = models.DateTimeField(blank=True)
def __str__(self):
return self.title + ' by ' + self.author
class Category(models.Model):
name = models.CharField(max_length=50)
class Meta:
verbose_name_plural = "categories"
def __str__(self):
return self.name
class BlogComment(models.Model):
sno = models.AutoField(primary_key=True)
comment = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp = models.DateTimeField(default=now)
def __str__(self):
return self.comment[0:13] + "..." + "by " + self.user.username
视图.py
from django.shortcuts import render, redirect, get_object_or_404
from blog.models import Post, BlogComment, Category
from django.contrib import messages
from blog.templatetags import extras
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
import socket
# Create your views here.
def blogHome(request, tag_slug=None):
allPosts = Post.objects.all().order_by("-timeStamp")
# context = {'allPosts': allPosts}
paginator = Paginator(allPosts, 5)
page = request.GET.get('page')
try:
allPosts = paginator.page(page)
except PageNotAnInteger:
allPosts = paginator.page(1)
except EmptyPage:
allPosts = paginator.page(paginator.num_pages)
return render(request, 'blog/blogHome.html', {'allPosts': allPosts, page:'pages'})
def blogCategory(request, pk):
category = get_object_or_404(Category, pk=pk)
return render(request, "blog/blogCategory.html", {'category': category})
def blogPost(request, slug):
post = Post.objects.filter(slug=slug).first()
post.views = post.views+1
post.save()
# comments
comments = BlogComment.objects.filter(post=post, parent=None)
replies = BlogComment.objects.filter(post=post).exclude(parent=None)
replyDict = {}
for reply in replies:
if reply.parent.sno not in replyDict.keys():
replyDict[reply.parent.sno] = [reply]
else:
replyDict[reply.parent.sno].append(reply)
context = {'post': post, 'comments': comments, 'user': request.user, 'replyDict': replyDict}
return render(request, 'blog/blogPost.html', context)
def postComment(request):
if request.method == 'POST':
comment = request.POST.get('comment')
user = request.user
postSno = request.POST.get('postSno')
post = Post.objects.get(sno=postSno)
parentSno = request.POST.get('parentSno')
# new comment
if parentSno == "":
comment = BlogComment(comment=comment, user=user, post=post)
comment.save()
messages.success(request, 'Your comment has been posted successfully')
# replies
else:
parent = BlogComment.objects.get(sno=parentSno)
comment = BlogComment(comment=comment, user=user, post=post, parent=parent)
comment.save()
messages.success(request, 'Your reply has been posted successfully')
return redirect(f'/blog/{post.slug}')
网址.py
from django.contrib import admin
from django.urls import path, include
from . import views
urlpatterns = [
path('postComment', views.postComment, name='postComment'),
path('', views.blogHome, name='blogHome'),
path('<str:slug>', views.blogPost, name='blogPost'),
path('category/<category>/', views.blogCategory, name='blogCategory'),
]
这是我的blogCategory.html模板(如果我错误地渲染模板,请也帮助我)
{% extends "base.html" %}
{% block title %} Categories {% endblock title %}
{% block blogactive %} active {% endblock blogactive %}
{% block body %}
<div class="container my-4">
<h2>Related Articles:</h2>
{% if posts == posts %}
<p>No such Category</p>
Your search did not match any documents.<br>
Suggestions: <br>
<ul>
<li>Make sure that all words are spelled correctly.</li>
<li>Try different keywords.</li>
<li>Try more general keywords.</li>
<li>Try fewer keywords.</li>
</ul>
{% endif %}
{% for category in post.categories.all %}
<div class="card mb-3">
<div class="card-body">
<div class="row g-0">
<div class="col-md-4">
<div class="ratio ratio-16x9">
<!-- featured image -->
<img src="{{ post.thumbnail.url }}" class="rounded featured-image-list" alt="{{post.title}}">
</div>
</div>
<div class="col-md-7 ps-md-3 pt-3 pt-md-0 d-flex flex-column">
<h2 class="card-title h3">
<a href="{{ post.slug }}">{{ post.title }}</a>
</h2>
<div class="text-muted">
<small>
Published {{ post.timeStamp }} by <strong>{{ post.author }}</strong>
</small>
</div>
<p class="card-text mb-auto py-2">{{ post.content|safe|striptags|truncatechars:300 }}</p>
<div>
<a href="{{ post.slug }}" class="btn btn-primary">Read more</a> | Categories:
{% for category in post.categories.all %}
<a href="{{ category.name }}">
{{ category.name }}
</a>
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock body %}
我的期望:当任何人点击标签时,他们应该看到该类别的博客文章
引发错误,因为
post = Post.objects.filter(slug=slug).first()
返回 None 所以...
尝试使用下面的代码片段
post = Post.objects.filter(slug=slug).first()
if post is not None:
# Perform actions with the retrieved post
post.views = post.views + 1
post.save()