forms - Can i use a filtre in a template in django? -


i made loop allowing me display each page questions , answers utilisers. wonder if it's possible in template "filter" issues of page 1 example?

<form action="" method="get"> {{ form.as_p }}   {% page in pages %}<hr>     {{ page }}:       <br>{% reply in  page.reply_set.all %}<br> {{ reply.question }} --> {{ reply.answer }} (author : {{ reply.user }}) {% endfor %} {% endfor %} 

i have fields manytomany it's hard understand...

class question(models.model):     label = models.charfield(max_length=30)      def __str__(self):             return self.label   class page(models.model):     title = models.charfield(max_length=30)      def __str__(self):         return self.title  class reply(models.model):     page = models.manytomanyfield(page)     question = models.foreignkey(question)     user = models.foreignkey(personne)     answer = models.charfield(max_length=30)     creationdate = models.datetimefield(default=timezone.now(),blank=true, verbose_name="date de création")       def __str__(self):         return str(self.answer) 

you can see on screenshot have tree pages('name visit 1 visit 2 visit 3) , want first visit loop ? the resultt

the best page want based on id

edit : views.py

def access(request,instance):     replies = reply.objects.all()     questions = question.objects.values()     logged_user = get_logged_user_from_request(request)     numpages = page.objects.get(pk=instance)     pages = page.objects.all()     # pagesfilter = page.objects.get(pk=instance).reply_set.all()     form = replyform(request.get)     personnes = personne.objects.all()     if logged_user:         if len(request.get) > 0:             form = replyform(request.get)             if form.is_valid():                 form.save(commit=true)                 return httpresponseredirect('/reply')             else:                 return render_to_response('polls/access.html', {'pagesfilter':pagesfilter, 'numpages': numpages, 'personnes': personnes, 'replies': replies, 'questions': questions,'pages':pages, 'form': form})         else:             form = replyform()             return render_to_response('polls/access.html', {'pagesfilter':pagesfilter, 'numpages': numpages, 'personnes':personnes, 'replies': replies, 'questions': questions, 'pages':pages, 'form': form})     else:         return httpresponseredirect('/login') 

if know visit1 first element in loop can do:

{% page in pages %}<hr>   {% if forloop.first %}    {{ page }}:       <br>{% reply in  page.reply_set.all %}<br> {{ reply.question }} --> {{ reply.answer }} (author : {{ reply.user }}) {% endfor %}   {% endif %} {% endfor %} 

but best way, think, visit1 in view through query , send template.

edit

as view looks

numpages = page.objects.get(pk=instance) 

gets page want. so, in template use it:

{{ numpage }} {% reply in  numpage.reply_set.all %}     {{ reply.question }}--> {{ reply.answer }}(author : {{ reply.user }}) {% endfor %} 

Comments