我的問題是兩個階段,但它來自相同的Django模型。我試圖讓我的類別模型與我的請願模型正常工作。目前,Django分類模型和模板視圖問題
我得到這個「FieldError」錯誤,當我定義的查詢集爲我的「類別查看」類:
Cannot resolve keyword 'created_on' into field. Choices are: description, id, petition, slug, title
下面是「類別查看」類的代碼:
class CategoryView(generic.ListView):
model = Category
template_name = 'petition/category.html'
context_object_name = 'category_list'
def get_queryset(self):
return Category.objects.order_by('-created_on')[:10]
以下是我的models.py中的代碼:
class Category(models.Model):
title = models.CharField(max_length=90, default="Select an appropriate category")
slug = models.SlugField(max_length=200, unique=True)
description = models.TextField(null=False, blank=False)
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.title
def get_absolute_url(self):
return "/categories/%s/"%self.slug
class Petition(models.Model):
title = models.CharField(max_length= 90, default="Enter petition title here")
created_on = models.DateTimeField(auto_now_add=True)
image = models.ImageField(null=False, upload_to=imageupload)
video = models.CharField(max_length=600, default="Enter an external video link")
petition = models.TextField(null=False, default="Type your petition here")
created_by = models.ForeignKey(User)
category = models.ManyToManyField(Category)
def total_likes(self):
return self.like_set.count()
def __str__(self):
return self.title[:50]
def get_signatures(self):
return self.signature_set.all()
我在我的類別視圖模板(類別)上得到'FieldError'當'get_queryset()'被定義時,
當我註釋它時,頁面顯示但帖子不被檢索;我得到一個類別列表,而不是。這裏是我的分類視圖模板(category.html):
{% include 'layout/header.html' %}
{% load humanize %}
<div class="container content">
<div class="row">
<div class="col-md-8 post-area">
{% if category_list %}
{% for petition in category_list %}
<div class="petition-block">
<h2 class="home-title"><a href="{% url 'detail' pk=petition.id %}">{{petition.title}}</a></h2>
<span class="petition-meta">
Created {{petition.created_on|naturaltime}} by
{% if petition.created_by == user %}
You
{% else %}
@{{ petition.created_by }}
{% endif %}
{% if petition.created_by == user %}
<a href="{% url 'editpetition' pk=petition.id %}">Edit</a>
{% endif %}
{% if petition.created_by == user %}
<a href="{% url 'deletepetition' pk=petition.id %}">Delete</a>
{% endif %}
</span>
{% if petition.image %}
<img src="{{ petition.image.url }}" alt="petition image" class="img-responsive" />
{% endif %}
</div><!--//petition-block-->
{% endfor %}
{% else %}
<p>Sorry, there are no posts in the database</p>
{% endif %}
</div>
<div class="col-md-4">
<h3>Topics</h3>
<ul>
{% for petition in category_list %}
<li><a href="#">{{petition.title}}</a></li>
{% endfor %}
</ul>
</div>
</body>
</html>
我在做什麼錯?請幫忙。
感謝您的迴應。該值在我的查詢與設置返回你的建議,我仍然得到同樣的錯誤:'無法解析關鍵字「petition_set__created_on」到field.' –
好吧,我把它改成: '高清get_queryset(個體經營): \t \t返回Category.objects.order_by(' - petition__created_on')[:10]'現在它的加載正常,除了模板(category.html)仍然返回類別列表而不是顯示類別中的帖子 –
啊,那是因爲你只是將類別傳遞給模板。看看listview是如何工作的http://ccbv.co.uk/projects/Django/1.9/django.views.generic.list/ListView/。我會編輯答案來完成你的問題 – harveydf