2017-09-21 3 views
0

djangoテンプレートでpython viewメソッドres()を呼び出そうとしていますが、呼び出されません。view.pyのPythonメソッドがdjangoテンプレートを呼び出さない

これは私がカウントをインクリメントしようとすると、以降のレベルの後に0にリセットしかし、そのリセット方法は、Djangoのテンプレートからの呼び出しを取得していないのです、私の見る

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) 

です。

これは

<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}}私のedit.htmlページでは、コールを取得していないとiterator_subtopicの値が再び0にセットされますされていません。 Functionとその呼び出しはTerminal上で正常に動作していますが、djangoテンプレートでは描画されません。

私が間違っている箇所を修正してください。 ありがとうございます。

答えて

0

あなたmake_incrementorクラスが__call__メソッドを持っているので、私は{{ iterator_subtopic.res }}が最初count整数を返します。これは、iterator_subtopic()を呼び出すと確信しています。次に、count整数(これは存在しない)のres属性にアクセスしようとするので、空の文字列''が出力されます。

0

私は単にのように私の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 %} 
。今私は私の期待される出力を得ています。

関連する問題