Ошибка шаблона в экземплярах модели - PullRequest
0 голосов
/ 01 января 2019

Я создаю блог с системой комментариев и тегов.Теперь я столкнулся с проблемой шаблона с экземплярами Model.Сначала я ставил статус для всех новых сообщений как «черновик», но теперь я установил статус «опубликован» и получил это сообщение об ошибке.Если вам нужно больше кода, пожалуйста, скажите мне.Я пытался наблюдать за поведением своего кода, когда добавлял в свой код новые элементы.

models.py

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


class Post(models.Model):
   STATUS_CHOICE = (
       ('draft', 'Draft'),
       ('published', 'Published')
    )
    title = models.CharField(max_length=250)
    slug = models.CharField(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_CHOICE,
                          default='published')

class Meta:
    ordering = ('-publish',)

def __str__(self):
    return self.title

objects = models.Manager()
published = PublishedManager()
tags = TaggableManager()

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

views.py

def post_detail(request, year, month, day, post):
    post = get_object_or_404(Post, slug=post,
                             status='published',
                             publish__year=year,
                             publish__month=month,
                             publish__day=day)

# List of active comment fot this post
comments = post.comments.filter(active=True)

new_comment = None

if request.method == 'POST':
    # A comment was posted
    comment_form = CommentForm(data=request.POST)
    if comment_form.is_valid():
        # Create Comment object bot don't save to database yet
        new_comment = comment_form.save(commit=False)
        # Assign the current post to the new comment
        new_comment.post = post
        # Save the comment to the database
        new_comment.save()
else:
    comment_form = CommentForm()
return render(request,
              'blog_site/post/detail.html',
              {'post': post,
               'comments': comments,
               'new_comment': new_comment,
               'comment_form': comment_form})


Template error:
In template Z:\Django\blog\blog_site\templates\blog_site\base.html, error 
at line 0
   Manager isn't accessible via Post instances
   1 : {% load static %}
   2 : <html lang="en">
   3 : <head>
   4 :     <meta charset="UTF-8">
   5 :     <meta name="viewport" content="width=device-width, initial- 
   scale=1">
   6 :     <title>{% block title %}{% endblock %}</title>
   7 :     <link href="{% static "css/blog_site.css" %}" rel="stylesheet">
   8 : </head>

base.html

{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>{% block title %}{% endblock %}</title>
    <link href="{% static "css/blog_site.css" %}" rel="stylesheet">
</head>
<body>
<div id="pattern"></div>
    <div id="header">
         <div class="menu" id="logo"><b>#blog</b></div>
         <div class="menu" id="nav_bar">
             <a href="{% url "blog_site:post_list" %}">Recently</a>
             <a href="{% url "blog_site:post_list" %}">Best of Month</a>
             <a>People</a>
         </div>
         <div class="menu" id="profile">Profile of user</div>
    </div>
    <div id="content">
        {% block content %}
        {% endblock %}
    </div>
</body>
</html>
...