我正在创建一个包含评论和标签系统的博客。现在,我遇到了Model实例的模板问题。首先,我将所有新帖子的状态都设置为“草稿”,但现在我将状态设置为“已发布”,并且收到了此错误消息。如果您需要更多代码,请告诉我。 当我向代码中添加新要素时,我试图观察代码的行为。
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])
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>