python - how to filter story by it's belonging tag -


i have 2 models news , category, , in news have foreignkey of category. know how display news same category in single template. furthermore, in home page i'm trying display featured news of each category. i'm having problem. models.py

class news(models.model):     title = models.charfield(max_length=120)     content = models.textfield()     category = models.foreignkey("tag")     active = models.booleanfield(default=true)     featured = models.booleanfield(default=false)     top = models.booleanfield(default=false)     slug = models.charfield(max_length=255, unique=true)     featuredincat = models.booleanfield(default=false)     objects = storymanager()    class newsqueryset(models.query.queryset):     def active(self):         return self.filter(active=true)     def featuredincat(self):         return self.filter(featuredincat=true)   class newsmanager(models.manager):     def get_queryset(self):         return newsqueryset(self.model, using=self._db)     def get_featuredincat(self):         return self.get_queryset().active().featuredincat()     def all(self):         return self.get_queryset().active()   class category(models.model):     title = models.charfield(max_length=120)     description = models.textfield(max_length=5000, null=true, blank=true) 

in views.py

def category_list(request):     categorylist = newscategory.objects.all()      featuredincat = news.objects.get_featuredincat()     context = {         "featuredincat":featuredincat         "categorylist":categorylist,   }     return render(request,"news/category_list.html", context) 

in template

  {% category in categorylist %} <div class='col-sm-4'> <div id="container">{{category.title}}</h1>     <ul>     {% x in featuredincat %}  <a href='{{ x.get_absolute_url }}'><li class="unique">{{x.title}}</li></a>     {% endfor %}     </ul>     </div>    <hr> </div> {% endfor %} 

then shows featuredincat in every category featuredincat should shown in category section. how fix this?

you can change loop iterate on correct objects

{% x in category.news_set.get_featuredincat %} 

you won't need context variable anymore


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -