Search code examples
djangochoicefieldhuman-readable

Display human readable choice in template


I have seen the posts on this already Display forms choice in template-Django

and they recommend using in the template {{ item.get_categories_display }} but my set up is a little different, so I can't access the human readable elements in categories.

Here is my view,

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)

The problem is in this

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

because it doesn't carry over all the info for display.

The template,

{% 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 %}

The model,

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

I just get black when I try to display {{ item.get_category_display }}

Any help would be greatly appreciated,

Thanks,


Solution

  • Keep the topic object in the dictionary:

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

    And your template:

    {% 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 %}