Django博客文章未出现
我是Django的新手,正在尝试返回已发布博客文章的列表。
该页面正在显示,并且没有错误产生,但是没有帖子显示。我已经检查了管理页面,并且所有帖子都已发布。
型号:
class BlogPost(models.Model):
title = models.CharField(max_length=200)
snippet = models.CharField(max_length=400, null=True)
Blog_pic = models.ImageField(upload_to='blog_pics',blank=True)
hero_pic = models.ImageField(upload_to='blog_pics',blank=True)
content = models.TextField()
blogcategory = models.CharField(max_length=200, default='uncategorised')
create_date = models.DateTimeField(default=timezone.now())
published_date = models.DateTimeField(blank=True, null=True)
#def publish(self):
# self.published_date = timezone.now()
# self.save()
def get_absolute_url(self):
return reverse("BlogPost_detail",kwargs={'pk':self.pk})
观看次数:
class BlogPostListView(ListView):
model = BlogPost
def get_queryset(self):
return BlogPost.objects.all().order_by('-published_date')
class BlogPostDetailView(DetailView):
model = BlogPost
模板:
{% for BlogPost in BlogPost_list %}
<div class="col-sm-6">
<a href="{% url 'BlogPost_detail' pk=BlogPost.pk %}">
<div class="card">
<img src="#">
<div class="">
<h4><span class="">{{ BlogPost.category }}</span></h4>
<p class="">{{ BlogPost.tile }}</p>
<p class="">{{ BlogPost.snippet }}</p>
<p style="">{{ BlogPost.published_date }</p>
<p style="">x min</p>
</div>
</div>
</a>
</div>
{% endfor %}