C
C
carrallton2020-04-27 01:27:36
Django
carrallton, 2020-04-27 01:27:36

Manager isn't accessible via Post instances what's wrong?

Hello, I will try to make a blog on the textbook. With the output of posts through the manager, it gives this error. The key to solving the problem is not in the book, I rewrote it in plain text.

models.py

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(status="published")

class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250, unique_for_date='publish')
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
    body = models.TextField()
    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 __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("blog:post_detail", args=[self.publish.year, self.publish.month, self.publish.day, self.slug])


views.py
def post_list(request):    
   posts = Post.published.all()
   return render(request, 'blog/list.html', {'posts': posts})

def post_detail(request):
    post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day)
    return render(request, 'blog/detail.html', {'post': post})


urls.py
urlpatterns = [
    path('', views.PostListView.as_view(), name="post_list"),
    path('<int:year>/<int:month>/<int:day>/<slug:post>/', views.post_detail, name='post_detail'),
]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim Shatalov, 2020-04-27
@carrallton

Error here

<p class="date">Published {{ post.published }} by {{ post.author }}</p>

replace with
<p class="date">Published {{ post.publish }} by {{ post.author }}</p>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question