Django模块温度过滤

2024-06-02 04:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我的英语道歉。 我是Django的一个新面孔,做我的第一个应用程序,同时学习。 路上有一些水泵,但到目前为止,这是一次愉快的旅行

我正在做的应用程序将是一个问卷调查。 这个想法是,你可以在管理面板下添加新的问题,然后在网站上访问者可以回答反馈的问题

为了学习,我想从头开始

我建立了一个模型,它由两部分组成:问号和问题本身。 我想知道怎样才能防止在模板中创建两个相同的问号。具有相同标签名的所有问题应显示在一个问题标签下,而不是重复本身

我要说明我的意思: 首先是代码:

型号.py

class QuestionTag (models.Model):
  tag_name = models.CharField(max_length=55)
  tag_description = models.CharField(max_length=200, blank=True)

  def __unicode__(self):
      return u'%s %s' % (self.tag_name, self.tag_description)

class Question (models.Model):
  QUEST_GROUP_NAME = (
      (1, 'Group1'),
      (2, 'Group2'),
  )
  question_group = models.IntegerField(max_length=3, choices=QUEST_GROUP_NAME, blank=False, default=1)
  question_tag = models.ForeignKey('QuestionTag')
  question_box = models.TextField()

  class Meta:
      ordering = ['-question_tag']

视图.py

class QuestionView(ListView, FormView):

    def get(self, request, person_id=1):

        if not person_id:
            return render(request, '404.html')

        try:
            personbyid = Person.objects.get(pk=person_id)
            questions = Question.objects.filter(question_group=1).order_by('question_tag')

        except Person.DoesNotExist:
            return render(request, '404.html')

        return render_to_response('questions.html',
                {'personbyid ': inetrviewerid ,'Question_group1': questions })

以及模板:

<form id='formID' action='' method='post' autocomplete="off">{% csrf_token %}

        {% for question in Question_group1 %}
          <div class='question_header'>
             <p class='question_tag'>.{{question.question_tag.tag_name}}</p>
             <p class='question_tag_descript'>({{question.question_tag.tag_description}})</p>
          </div>
      <label class="question_quest">{{question.question_box}}</label>
      <textarea name="answer" placeholder="Some text about the question!" rows="3"></textarea>
    {% endfor %}

 </form>

到目前为止我得到的是:

-问号(描述)

---问题一

---文本区域

-问号(描述)

---问题二

---文本区域


我怎么能让问题看起来像这样:

-问号(描述)

---问题一

---文本区域

---问题二

---文本区域

谢谢你


Tags: name文本selfid区域returnmodelstag
1条回答
网友
1楼 · 发布于 2024-06-02 04:21:33

您可以使用^{}模板标记:

{% for question in Question_group1 %}
    {% ifchanged question.question_tag %}
      <div class='question_header'>
         <p class='question_tag'>.{{question.question_tag.tag_name}}</p>
         <p class='question_tag_descript'>({{question.question_tag.tag_description}})</p>
      </div>
    {% endifchanged %}
  <label class="question_quest">{{question.question_box}}</label>
  <textarea name="answer" placeholder="Some text about the question!" rows="3"></textarea>
{% endfor %}

现在,ifchanged块的内容将仅在question\u标记更改时输出

相关问题 更多 >