【问题标题】:Python method from view.py not getting call on django templateview.py 中的 Python 方法没有在 django 模板上调用
【发布时间】:2018-03-03 00:11:28
【问题描述】:

我试图在 django 模板上调用 python 视图方法 res() 但它没有被调用。

这是我的观点

class make_incrementor(object):
       def __init__(self, start):
          self.count=start
       def __call__(self, jump=1):
          self.count += jump
          return self.count

       @property
       def res(self):
          self.count =0
          return self.count
def EditSchemeDefinition(request, scheme_id):

    iterator_subtopic = make_incrementor(0)
    scheme_recs = scheme.objects.get(id=scheme_id)
    view_val = {
        'iterator_subtopic': iterator_subtopic,
        "scheme_recs": scheme_recs,
    }
    return render(request, "edit.html", view_val)

我正在尝试增加计数,然后在一个级别之后将其重置为 0 但是它的重置方法没有从 Django 模板中获得调用。

这是我的 edit.html 页面

<td id="subTopic" class="subTopic">
    {% for strand in  scheme_recs.stand_ids.all %}
        {{ iterator_subtopic.res }}
        {% for sub_strand in  strand.sub_strand_ids.all %}
            {% for topic in  sub_strand.topic_ids.all %}
                {% for subtopic in  topic.sub_topic_ids.all %}
                    <input id="subTopic{{ iterator_subtopic }}" class="len"
                           value="{{ subtopic.name }}">
                    <br>
                {% endfor %}
            {% endfor %}
        {% endfor %}
    {% endfor %}
</td>

我的 {{ iterator_subtopic.res }} 没有得到调用,并且 iterator_subtopic 的值没有再次设置为 0。函数及其调用在终端上运行良好,但在 django 模板中无法呈现。

请纠正我做错的地方。 提前致谢。

【问题讨论】:

    标签: html django python-2.7


    【解决方案1】:

    由于你的make_incrementor 类有一个__call__ 方法,我相信{{ iterator_subtopic.res }} 会首先调用iterator_subtopic(),它会返回count 整数。然后它将尝试访问count 整数(不存在)的res 属性,因此它将输出空字符串''

    【讨论】:

      【解决方案2】:

      我只是修改了我的 make_incrementor 类方法,例如:-

      class make_incrementor(object):
      count = 0
      
      def __init__(self, start):
          self.count = start
      
      def inc(self, jump=1):
          self.count += jump
          return self.count
      
      def res(self):
          self.count = 0
          return self.count
      
      def EditSchemeDefinition(request, scheme_id):
      
      iterator_subtopic = make_incrementor(0)
      scheme_recs = scheme.objects.get(id=scheme_id)
      view_val = {
          'iterator_subtopic': iterator_subtopic,
          "scheme_recs": scheme_recs,
      }
      return render(request, "edit.html", view_val)
      

      稍后在我的 django 模板中,我将其方法称为:-

      <td id="subTopic" class="subTopic">
      <p hidden value="{{ iterator_subtopic.res }}"></p>
      {% for strand in  scheme_recs.stand_ids.all %}
          {{ iterator_subtopic.res }}
          {% for sub_strand in  strand.sub_strand_ids.all %}
              {% for topic in  sub_strand.topic_ids.all %}
                  {% for subtopic in  topic.sub_topic_ids.all %}
                      <input id="subTopic{{ iterator_subtopic.inc  }}" class="len"
                             value="{{ subtopic.name }}">
                      <br>
                  {% endfor %}
              {% endfor %}
          {% endfor %}
      {% endfor %}
      

      它完成了。现在我得到了预期的输出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-27
        • 2020-04-10
        • 1970-01-01
        • 2014-04-06
        • 2021-02-28
        • 2011-03-27
        • 1970-01-01
        • 2020-05-29
        相关资源
        最近更新 更多