当我在href中使用post.get_absolute_url时,我得到了这个错误.如何解决这个问题



AttributeError at/blog/无法通过Post实例访问Manager

模板呈现过程中出错在模板C:\Users\Mahdyar Eatemad\OneDrive\Projects\Gallery\templates\blog\post\list.html中,第33行出现错误

无法通过Post实例访问Manager

错误

我用get_absolute_url为博客文章创建了slug

这是我的型号

from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from django.urls import reverse
# Create your models here.
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset().filter(status='published')
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'چرک نویس'),
('published', 'منتشر شده'),
)
title                = models.CharField(max_length=100)
slug                 = models.SlugField(max_length=100, unique_for_date='publish', allow_unicode=True)
body                 = models.TextField()
author               = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
bolded_text          = models.TextField(blank=True)
picture              = models.ImageField(upload_to='photos', blank=True)
publish              = models.DateTimeField(default=timezone.now)
created              = models.DateTimeField(auto_now_add=True)
updated              = models.DateTimeField(auto_now=True)
status               = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')
objects = models.Manager()
published = PublishedManager()
class Meta:
ordering = ('-publish',)

def get_absolute_url(self):
return reverse('blog:post_detail', args=[self.published.year, self.published.month, self.published.day, self.slug])
def __str__(self):
return self.title

我的观点

from django.shortcuts import render, get_object_or_404
from .models import Post
# Create your views here.
def post_list(request):
posts = Post.published.all()
return  render(request, 'blog/post/list.html', {'posts': posts})
def post_detail(request, year, month, day, slug):
post = get_object_or_404(Post, slug=slug, status='published', published__year=year,     published__month=month, published__day=day)
return render(request, 'blog/post/list.html', {'post': post})

我的url

from django.urls import path, register_converter
from django.urls.converters import SlugConverter
from . import views
app_name = 'blog'
class PersianSlugConvertor(SlugConverter):
regex = '[-a-zA-Z0-9_ضصثقفغعهخحجچگکمنتالبیسشظطزژرذدپوءآ]+'
register_converter(PersianSlugConvertor, 'persian_slug')
urlpatterns = [
path('', views.post_list, name='post_list'),
path('<int:year>/<int:month>/<int:day>/<persian_slug:slug>', views.post_detail,     name='post_detail'),
]

模板

{% for post in posts %}
<article class="post">
<div class="post_header">
<h3 class="post_title"><a href="{{ post.get_absolute_url }}">{{ post.title }}</a></h3>
<div class="post_sub"><i class="fa-time"></i>منتشر شده در {{ post.publish }} توسط {{ post.author }}<a href="single_post.html#post_comments"><i class="fa-comments-alt"></i> 6 نظر</a> </div>
</div>
<div class="post_content">
<figure><a href="{{ post.get_absolute_url }}"><img alt="عکس پست" src="{{ post.picture }}"></a></figure>
<p>{{ post.body|truncatewords:30|linebreaks }}</p>
<a href="{{ post.get_absolute_url }}" class="btn btn-primary">بیشتر بخوانید</a> </div>
</article>
{% endfor %}

您编写

reverse('blog:post_detail', args=[self.published.year, 
self.published.month, self.published.day, self.slug])

如果您引用的是管理器published,则您希望引用日期时间字段

最新更新