2017-08-11 10 views
0

私はDjangoアプリケーションを構築していますが、実行するたびにidを持つ特定のdiv要素の内側のhtmlにhtmlの行を追加するビュー/関数が必要です。DjangoでDOM要素を生成または変更するにはどうすればよいですか?

私はJavascriptでこれを行う方法を知っていますが、実行するたびにそのスクリプトを実行するにはビューが必要です。 Pythonドキュメントでこれを行う方法はありますか?

+0

このスクリプトを実行する特定のURLにajaxのリクエリーを使用できます –

答えて

0

あなたの質問のタイトルは、PythonでDOM要素を作成することについて質問します。そうするためには、あなたのviews.py内部

from django.template import Context, Template 

def in_some_view(request): 
    template = Template('<div class="new-dom-element">Element</div>') 
    ''' 
    You can create elements above. You can even give it Context Variables 
    For example: template = Template('<div class="new-dom-element">{{ context_variable }}</div>') 
    ''' 
    context = Context({'context_variable': your_python_variable}) 
    #By sending the context variable "your_python_variable", you can do anything iteratively. 

    dom_element = template.render(context) 
    #Now you can return this dom_element as an HttpResponse to your Javascript. 
    #Rest of your code. 
    return HttpResponse(dom_element) 

注:私はAJAXのドメインがを要求外の方法を取るしたくない心の中で、この維持に答え。もしそうなら、HTMLが既にブラウザ画面に表示されている場合、いくつかのデータを持ち込んで追加することは本当に困難です。これがAJAXが最初に作られた理由です。

これが役に立ちます。ありがとう。

関連する問題