【发布时间】:2021-06-28 00:16:44
【问题描述】:
我有这个模型
class Post(models.Model):
title = models.CharField(max_length=100)
title2 = models.CharField( max_length=100)
content = models.TextField(default=timezone.now)
content2 = models.TextField(default=timezone.now)
post_image = models.ImageField(upload_to='post_pics')
post_image2 = models.ImageField(upload_to='post2_pics')
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
以及使用该模型的基于函数的视图:
class PostListView(ListView):
model = Post
template_name = 'front/front.html'
context_object_name = 'listings'
ordering = ['-date_posted']
def get_context_data(self, **kwargs):
check_for_zipcode = #where I want to access the author for the current instance
context = super().get_context_data(**kwargs)
context['zipcodes'] = check_for_zipcode
return context
我只想知道如何在基于类的视图中访问author 字段。我可以像这样在我的 HTML 中访问作者”
{% for listings in listings %}
<h3>listings.author</h3>
{% endfor %}
这样,我将为该模型的每个实例取回 author 字段。
如何获取变量check_for_zipcode 中实例的作者?我试过self.author、self.listings.author等;但没有任何效果
【问题讨论】:
标签: python django django-views django-forms django-templates