如何在模板中显示ManyToMany字段

时间:2016-02-09 09:19:16

标签: python django templates many-to-many

我想知道如何在ManyToMany字段中检索我的模板... 例如,在我的模型中,我想检索与每个问题相对应的页面并将其发布到模板中... 我的班级页面中有4个值("访问1访问2访问3,4访问") 我在我的数据库中将每个问题与"访问"

联系起来

我希望我现在可以看到我的模板中对任何问题的访问方式。

我将分享我的模特:

class Page(models.Model):
    title = models.CharField(max_length=30)


    def __str__(self):
        return self.title

class Question(models.Model):
    label = models.CharField(max_length=30)
    page = models.ManyToManyField(Page)

    def __str__(self):
            return self.label

class Reply(models.Model):
    question = models.ForeignKey(Question)
    user = models.ForeignKey(Personne)
    answer = models.CharField(max_length=30)
    creationDate = models.DateTimeField(default=datetime.datetime(2016, 1, 20, 15, 4, 21, 467165))

    def __str__(self):
        return str(self.answer)

我的模板:

{% for reply in replies %}<br>
    <br> {{ reply.user }}
    {{ reply.question }} -
    {{ reply.answer }}
    (dans la :{% for page in questions.page %} {{ page }}) {% endfor %}  #this is this line who doesn't work 
{% endfor %}

如何在模板页面中调用特定问题?

  

编辑:

def reply(request):
    replies = Reply.objects.all()
    questions = Question.objects.all()
    logged_user = get_logged_user_from_request(request)
    pages = Page.objects.all()
    form = ReplyBisForm(request.GET)
    personnes = Personne.objects.all()
    if logged_user:
        if len(request.GET) > 0:
            form = ReplyBisForm(request.GET)
            if form.is_valid():
                form.save(commit=True)
                return HttpResponseRedirect('/reply')
            else:
                return render_to_response('polls/reply.html', {'personnes': personnes, 'replies': replies, 'questions': questions,'pages':pages, 'form': form})
        else:
            form = ReplyBisForm()
            return render_to_response('polls/reply.html', {'personnes':personnes, 'replies': replies, 'questions': questions, 'pages':pages, 'form': form})
    else:
    return HttpResponseRedirect('/login')

1 个答案:

答案 0 :(得分:3)

由于sendSourceAndReceiveToResult(partnerURI, source, new WebServiceMessageCallback() { public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException { StringSource mefHeaderSource = new StringSource( header); SoapHeader soapHeader = ((SoapMessage) message) .getSoapHeader(); Transformer transformer = TransformerFactory .newInstance().newTransformer(); transformer.transform(mefHeaderSource, soapHeader.getResult()); } }, result); 字段已针对每个模型实例转换为RelatedManager,因此您应指定相关对象的子集,或者只使用all()

ManyToMany