1
我做了一個模板,把一些崗位上,每個崗位有兩個類別,我用<a>
是選項,如:使用Django過濾器,但不工作?
<div class="cate">
<span class="cate_span0">Species:</span>
<a href="?kind={{ post_kind }}">
<span class="cate_span {% ifequal animal_kind '' %}chosen_animal{% endifequal %}">All</span>
</a>
<a href="?animal=dog&kind={{ post_kind }}">
<span class="cate_span {% ifequal animal_kind 'dog' %}chosen_animal{% endifequal %}">Dog</span>
</a>
<a href="?animal=cat&kind={{ post_kind }}">
<span class="cate_span {% ifequal animal_kind 'cat' %}chosen_animal{% endifequal %}">Cat</span>
</a>
<a href="?animal=other&kind={{ post_kind }}">
<span class="cate_span {% ifequal animal_kind 'other' %}chosen_animal{% endifequal %}">other</span>
</a>
<br/>
<span class="cate_span0">Article:</span>
<a href="?animal={{ animal_kind }}">
<span class="cate_span {% ifequal post_kind '' %}chosen_animal{% endifequal %}">All</span>
</a>
<a href="?kind=new_explorer&animal={{ animal_kind }}">
<span class="cate_span {% ifequal post_kind 'new_explorer' %}chosen_animal{% endifequal %}">Stray</span>
</a>
<a href="?kind=tell_story&animal={{ animal_kind }}">
<span class="cate_span {% ifequal post_kind 'tell_story' %}chosen_animal{% endifequal %}">Story</span>
</a>
<a href="?kind=giving&animal={{ animal_kind }}">
<span class="cate_span {% ifequal post_kind 'giving' %}chosen_animal{% endifequal %}">Post</span>
</a>
<a href="?kind=want&animal={{ animal_kind }}">
<span class="cate_span {% ifequal post_kind 'want' %}chosen_animal{% endifequal %}">Want</span>
</a>
</div>
在我的觀點我用kind
和animal
過濾數據庫:
animal_kind = request.GET.get('animal', '')
post_kind = request.GET.get('kind', '')
if animal_kind:
all_posts = all_posts.filter(animal_cate=animal_kind)
if post_kind:
all_posts = all_posts.filter(post_cate=post_kind)
和:
return render(request, 'community.html', {
"all_posts" : posts,
"animal_kind" : animal_kind,
"post_kind" : post_kind,
})
我的模型:
class Posts(models.Model):
POST_CATE_CHOICES = [
('GV', 'giving'),
('WT', 'want'),
('NE', 'new_explorer'),
('TS', 'tell_story'),
]
ANIMAL_CATE_CHOICES = [
('DOG', 'dog'),
('CAT', 'cat'),
('OTHER', 'other'),
]
user = models.ForeignKey(UserInfo, related_name='user_name', verbose_name=u"所屬用戶")
icon = models.ForeignKey(UserInfo, related_name='user_icon', verbose_name=u"用戶頭像")
title = models.CharField(max_length=100, verbose_name=u"帖子標題")
content = models.TextField(null=True, blank=True, verbose_name=u"帖子內容")
add_time = models.DateTimeField(default=datetime.now, verbose_name=u"發帖時間")
image = models.ImageField(max_length=100, upload_to='post_img/%Y/%m', null=True, verbose_name=u"貼圖")
post_cate = models.CharField(max_length=2, choices=POST_CATE_CHOICES, verbose_name="帖子類型")
animal_cate = models.CharField(max_length=10, choices=ANIMAL_CATE_CHOICES, null=True, verbose_name="動物類型")
comment_num = models.IntegerField(default=0, verbose_name=u"評論數量")
likes = models.IntegerField(default=0, verbose_name=u"頂")
dislikes = models.IntegerField(default=0, verbose_name=u"踩")
我的帖子:
{% for post in all_posts.object_list %}
<div class="post">
<a href="" class="user_a">
<img class="icon" src="{{ post.icon.icon.url }}">
<span class="name_span">{{ post.user.username }}</span>
</a>
<span class="time">發表時間:{{ post.add_time }}</span>
<span class="clicked">回覆:{{ post.comment_num }}</span>
<a href="">
<img class="post_img" src="{{ MEDIA_URL }}{{ post.image }}">
<div class="article">
<h3 class="title">{{ post.title }}</h3>
<span class="content"> {{ post.content }}</span>
</div>
</a>
</div>
{% endfor %}
好像只有animal_kind
實際工作,它可以過濾確切的項目,但是當我篩選post_cate
,它是空的。 我不問問題在哪裏,也許是我的看法,因爲當我選擇該選項時,網址是正確的。
請您鞋模型和崗位 –
我把我的崗位模型和後DIV – Klawens
爲post_cate你在路過?像GV還是給予?和爲animal_cate你通過網址狗或狗通過什麼? – Exprator