When i uses post.get_absolute_url in href i got this error. how fix this?

AttributeError at /blog/ Manager isn't accessible via Post instances

Error during template rendering In template C:\Users\Mahdyar Eatemad\OneDrive\Projects\Gallery\templates\blog\post\list.html, error at line 33

Manager isn't accessible via Post instances


i used get_absolute_url to create slug for blog posts

This is my models

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):
    ('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

my views

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})

my 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 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>
{% endfor %}

you write

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

where you are referencing published which is a manager, instead you want to refer to publish which is your date time field

Back to Top