2016-06-20 4 views
0

に私はこのすでに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 

I、表示しようとすると、私は黒取得{{item.get_category_displayは}}

任意の助けをいただければ幸い、

ありがとう、

+0

はい、私は疑問に思う他の方法があります。ありがとう –

+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 %} 
+0

ああ、申し訳ありません私は私のモデルを見せていませんでした。私は今それを追加します。これを見てくれてありがとう、私は今それを試してみます。 –

+0

残念ながら{{item.category.get_category_display}}は何も表示していません。 –

+0

{{item.category.get_categories_display}}も試しましたが、まだ何も表示していません。 –

関連する問題