0

「current.html」でajax経由からプルしようとしています

フォームを処理するビューは「form.html」をレンダリングします

「current.html」は次のようなものです。

<script>
    // ajax that pulls the form on a#get_form click 
    // fills up div#mod_post with the result 
</script>

<a id="get_form" href="{% url to the view that handles the form %}">get form</a>

<div id="mod_post" style="display:none;">
    {% include "form.html" %}
</div>

ここまではすべて問題ありません...フォームが適切に取得されて表示されます。

「form.html」は次のようなものです。

<script type="text/javascript" src="/the/script/that/should/handle/the/post.js"></script>

<form id="mod_form" action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
    <table class="form">
        {{ form.as_table }}
    </table>
    <input class="submit" type="submit" value="modifica">
</form>

唯一のことは、form.htmlがレンダリングされるときはいつでも、その部分が含まれることは決してないということです。

<script type="text/javascript" src="/the/script/that/should/handle/the/post.js">
</script>

私はここで何か間違ったことをしていますか、明らかにそうですが、それを見つけることはできません。

フィードバックは大歓迎です、ありがとう!

4

2 に答える 2

0

JavaScriptを介してHTML応答を直接ダンプしている場合、それは正常な動作です。ブラウザ(つまり、それらすべて)は<script>、セキュリティ上の予防措置として、任意のHTML文字列応答を介してJSによってページに追加されたタグを自動ストリップします。

于 2012-05-07T19:31:29.353 に答える
0

フォーム内に関連するjs/cssスクリプトをカプセル化することをお勧めします。例えば

class MyForm(forms.Form):
    field1 = ...
    field2 = ...
    class Media:
        js = ('script_that_handles_the_form.js',)

あなたのbase.html中には次のようなブロックがあります:

{% block scripts %}
  <!--- some scripts that apply to all pages -->
{% endblock %}

そしてあなたのform.html

{% block scripts %}
  {{ block.super }}    # retain the standard scripts that apply everywhere
  {{ form.media }}     # add the form specific media/scripts
{% endblock %}

<form id="mod_form" action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
    <table class="form">
        {{ form.as_table }}
    </table>
    <input class="submit" type="submit" value="modifica">
</form>
于 2012-05-07T19:49:24.003 に答える