2012-02-04 4 views
0

辞書にテンプレートがあり、テンプレートローダーでそれらを取得したいと思います。しかし、Djangoコードでインスタンス化されているので、その時点で辞書を渡すことはできません。カスタムDjangoテンプレートローダに変数を渡すにはどうすればいいですか?

テンプレートローダーに変数を渡す適切な方法は何ですか? (辞書を動的に構築されているので、私はDjangoの設定ファイルを使用することはできません。)

マイローダコードを、私はまだdictionaryを使用することはできません。

class Loader(BaseLoader): 
    is_usable = True 

    def load_template_source(self, template_name, template_dirs=None): 
     if template_name in dictionary: 
      return (dictionary[template_name],template_name) 
     raise TemplateDoesNotExist("Could not find template '%s'." % template_name) 

    load_template_source.is_usable = True 

答えて

0

問題は何ですか?これはうまくいくはずです。

それは動的にする必要がある場合だけdictionaryを移入する機能で投げる..どこか

dictionary = {'template_name' : 'template content'} 

class Loader(BaseLoader): 
    is_usable = True 

    def load_template_source(self, template_name, template_dirs=None): 
     if template_name in dictionary: 
      return (dictionary[template_name],template_name) 
     raise TemplateDoesNotExist("Could not find template '%s'." % template_name) 

dictionaryを定義します。

class Loader(BaseLoader): 
    is_usable = True 

    def load_template_source(self, template_name, template_dirs=None): 
     if template_name in self.get_dictionary(): 
      return (dictionary[template_name],template_name) 
     raise TemplateDoesNotExist("Could not find template '%s'." % template_name) 

    def get_dictionary(self): 
     dynamic = {} 
     # magically dynamically populate the dictionary 
     return dynamic 
+0

私は別の関数で辞書にデータを設定しなければならなかったので、それに割り当てる前に 'グローバル辞書 'を追加するのを忘れました。助けてくれてありがとう。 – fgm2r

関連する問題