在模板中显示人类可读的选择

时间:2016-06-20 12:36:43

标签: django choicefield human-readable

我已经在Display forms choice in template-Django

上看过这个帖子了

他们建议在模板{{item.get_categories_display}}中使用,但我的设置稍有不同,所以我无法访问类别中的人类可读元素。

以下是我的观点,

def forum(request, categ):
    postModelAll = PostModel.objects.all()
    postModel = PostModel.objects
                .filter(topic__categories = cater)
                .values('topic_id')
                .annotate( max=Max('pub_date'), 
                 freq=Count('topic_id'), 
                 contributors=Count('author', distinct=True))
                .order_by('-max')

    columnlist = []
    for item in postModelAll:
         columnlist.append([item])


    for item in postModel:
        for i in range(len(columnlist)):
            if item['max'] == columnlist[i][0].pub_date:
                item['author'] = columnlist[i][0].topic.author
                item['authorid'] = columnlist[i][0].topic.author_id
                item['topic'] = columnlist[i][0].topic.topic
                item['category'] = columnlist[i][0].topic.categories
                item['post'] =  columnlist[i][0].post
                item['views'] = columnlist[i][0].topic.views


context = {'forum_model': forum_model, 'current_time': timezone.now()}
return render(request, 'forum_by_category.html', context)

问题在于此

line item['category'] = columnlist[i][0].topic.categories

因为它不会带来所有显示信息。

模板,

{% for item in forum_model %}
        <tr>
          <tr>
            <td>{{ item.get_category_display }}</td>
            <td>{{ item.topic_id }}</td>
            <td><a href="{% url 'thread' item.topic_id %}"><span title="{{ item.post|slice:':300' }}">{{ item.topic }}</span></a></td>
            <td class="table-cell-center"><a href="{% url 'profile' item.authorid %}"><span title="{{ item.author }}'s profile">{{ item.author }}</span></a></td>
            <td class="icon-nowrap"><span title="published {{ item.max }}">{{ item.max|timesince:current_time}}</span></td>
            <td class="table-cell-center">{{ item.views }}</td>
            <td class="table-cell-center">{{ item.freq }}</td>
            <td class="table-cell-center">{{ item.contributors }}</td>
            <td>votes</td>
          </tr>
        </tr>
        {% endfor %}

模特,

class TopicModel(models.Model):
    topic = models.CharField(max_length = 100)
    topicAuthor = models.CharField(max_length = 100)
    author = models.ForeignKey(User)
    GeneralHelp = 'GH'
    SubmittingPortfolios = 'SP'
    GeneralTeaching = 'GT'
    Level12 = 'L12'
    Level3 = 'L3'
    Level4 = 'L4'
    Level5 = 'L5'
    Level6 = 'L6'
    forum_categories = (
        (GeneralHelp, 'General Help'),
        (SubmittingPortfolios, 'Submitting Portfolios'),
        (GeneralTeaching, 'General Teaching'),
        (Level12, 'Level 1 & 2'),
        (Level3, 'Level 3'),
        (Level4, 'Level 4'),
        (Level5, 'Level 5'),
        (Level6, 'Level 6'),
    )
    categories = models.CharField(
        max_length=30,
        choices=forum_categories,
        default=Level4,
    )


    views = models.PositiveIntegerField(default = 0)
    def __str__(self):              # __unicode__ on Python 2
            return self.topic

当我尝试显示{{item.get_category_display}}

时,我才变黑

非常感谢任何帮助,

谢谢,

1 个答案:

答案 0 :(得分:1)

topic对象保留在字典中:

if item['max'] == columnlist[i][0].pub_date:
    item['post'] =  columnlist[i][0].post
    item['topic'] = columnlist[i][0].topic

你的模板:

{% for item in forum_model %}
<tr>
  <tr>
    <td>{{ item.topic.get_categories_display }}</td>
    <td>{{ item.topic_id }}</td>
    <td><a href="{% url 'thread' item.topic.topic_id %}"><span title="{{ item.post|slice:':300' }}">{{ item.topic.topic }}</span></a></td>
    <td class="table-cell-center"><a href="{% url 'profile' item.topic.author_id %}"><span title="{{ item.topic.author }}'s profile">{{ item.topic.author }}</span></a></td>
    <td class="icon-nowrap"><span title="published {{ item.max }}">{{ item.max|timesince:current_time}}</span></td>
    <td class="table-cell-center">{{ item.topic.views }}</td>
    <td class="table-cell-center">{{ item.freq }}</td>
    <td class="table-cell-center">{{ item.contributors }}</td>
    <td>votes</td>
  </tr>
</tr>
{% endfor %}
相关问题