0

いくつかのモデル入力フィールドを使用して特定のクエリを作成しようとしています。 私は以下のモデルのエントリを持っています。ここテンプレート内のdjangoクエリーセット

models.py

class Work(models.Model): 

    categories =(
       ('cat1', 'cat1'), 
       ('cat2', 'cat2'), 
       ('cat3', 'cat3'), 
       ('cat4', 'cat4'), 
       ('cat5', 'cat5'), 
       ) 
    title = models.CharField(max_length=200) 
    description = RichTextUploadingField(config_name='awesome_ckeditor') 
    date = models.DateTimeField(default=timezone.now) 
    category = models.CharField(max_length=200, choices = categories, default = 'projects') 
    thumb = models.ImageField(upload_to = 'works/thumbs', blank = True) 
    content = models.FileField(upload_to = 'works/content_media', blank = True) 
    published = models.BooleanField() 

    def __str__(self): 
     return self.title 

    def get_absolute_url(self): 
     return reverse("work_detail",kwargs={'pk':self.pk}) 
    @property 
    def thumb_url(self): 
     if self.thumb and hasattr(self.thumb, 'url'): 
      return self.thumb.url 
    @property 
    def content_url(self): 
     if self.content and hasattr(self.content, 'url'): 
      return self.content.url 

は図である。

views.py

class WorksListView(ListView): 
    template_name = 'template.html' 
    model = Work 

    def get_queryset(self): 
     return Work.objects.filter(published=True).order_by('-date') 

と私がしようとしています最初にカテゴリフィールドで照会し、次のテンプレートに入力します。

template.html

{% for category in works_list.category %} 
    <ul data-category-name={{category.name}}> 
    {% for work in category.works %} 
     <li data-thumbnail-path={{thumbnail.url}} data-url={{content.url}} > 
      <div> 
       <p class="gallery1DecHeader">{{work.title}}</p> 
       <p class="gallery1DescP">{{work.description}}</p> 
      </div> 
     </li> 
    {% endfor %} 
{% endfor %} 

何私は変更する必要がありますか?

+0

、[リストビューのドキュメント]を追加(https://でdocs.djangoproject.com/en/1.11/topics/class-based-views/generic-display/)は、「テンプレートはobject_listという変数を含むコンテキストに対してレンダリングされる」と述べています。だから、あなたのテンプレートはworks_listが何であるか分からないようです。ありがとう@Sam。 –

答えて

0

私が見ることができるものから、いくつかの問題があります。まず、context_object_name = 'works_list'を追加してください。テンプレートouter forループの場合のように、object_listworks_listとすることができます。より大きな問題はworks_list.categoryを反復しており、WorkモデルによればCharlist()です。 choices kwargが何をしているのかと混乱している可能性があり、{% for category in works_list.category %}があなたのchoicesを繰り返し、あなたが定義した猫のリストをcategoriesに与えることを期待していると思います。私が知る限り、それは選択の仕方ではありません。

管理パネルにアクセスして仕事モデルの新しいエントリを追加すると、カテゴリにはあなたの猫のリストを含むドロップダウンリストが表示されます。したがって、選択肢は、既存のWorkオブジェクトのリストではなく、新しいWorkオブジェクトの合法的なカテゴリオプションのセットを定義します。

私が実際に望むのは、Categoryという追加のモデルです:work = models.ForeignKey(Work, on_delete=models.CASCADE)は1対多の関係です。基本的に、Workには、反復可能なオブジェクトのサブセットCategoryが必要です。これには、データの構造とアクセス方法を再設計する必要があります。

+0

コンテキストオブジェクトの部分を取得しましたが、残りの部分を変更する方法の例を示すことができますか? – Pierre

0

少なくとも、あなたのviews.pyとtemplate.htmlを変更する必要があります。 context_object_nameとスターターのための余分なコンテキスト(Doc Link

views.py

class WorksListView(ListView): 
    template_name = 'template.html' 
    model = Work 
    context_object_name = 'work_list' 

    def get_queryset(self): 
     return Work.objects.filter(published=True).order_by('-date') 

    def get_context_data(self, **kwargs): 
     # Call the base implementation first to get a context 
     context = super(WorksListView, self).get_context_data(**kwargs) 
     # Insert categories so that it can be used in template 
     context['categories'] = Work.categories 
     return context 

template.html

{% for category in categories%} 
<ul data-category-name={{category.0}}> 
    {% for work in work_list %} 
     {% if category.0 == work.category %} 
      <li data-thumbnail-path={{work.thumb_url}} data-url={{work.content_url}} > 
       <div> 
        <p class="gallery1DecHeader">{{work.title}}</p> 
        <p class="gallery1DescP">{{work.description}}</p> 
       </div> 
      </li> 
     {% endif %} 
    {% endfor %} 
</ul> 
{% endfor %} 
関連する問題