使用Django过滤但不工作?

时间:2017-07-27 06:43:50

标签: django html5 django-views

我制作了一个模板,在其上添加了一些帖子,每个帖子都有两个类别,我使用<a>作为选项,例如:

    <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>

在我的观看中,我使用kindanimal过滤了数据库:

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">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ post.content }}</span>
    </div>
    </a>
</div>
{% endfor %}

似乎只有animal_kind实际上有效且它可以过滤确切的项目,但是当我过滤post_cate时,它是空的。 我不知道问题所在,也许是我的观点,因为当我选择该选项时,网址是正确的。

1 个答案:

答案 0 :(得分:1)

过滤器未返回任何值,因为匹配的值不存在于数据库中,

您正在GV database中存储post_cate,但animal_cate存储的dog 'DOG'类似于密钥('GIVE','give')因此它正在发挥作用。

因此您需要将post_cate选项的格式更改为<asp:Chart ID="Chart1" runat="server" DataSourceID="Sql" Width="700px"> <Series> <asp:Series Name="Series1" XValueMember="BusinessLine" YValueMembers="% Completed" ChartType ="Column" ></asp:Series> </Series> <ChartAreas> <asp:ChartArea Name="ChartArea1"> <AxisX Interval="1" TextOrientation="Rotated90"> </AxisX> </asp:ChartArea> </ChartAreas> </asp:Chart>