0

フォームを含む html ページがあり、フォームが正常に送信されたときに、以下の div を表示します。

<div class="response" style="display: none;">
  <p>you can download it<a href="{{ link }}">here</a></p>
</div>

jquery関数もあります:

    <script type="text/javascript">
        $(function() {
            $('#sendButton').click(function(e) {            
                e.preventDefault();
                var temp = $("#backupSubmit").serialize();
                validateForm();
                $.ajax({
                    type: "POST",
                    data: temp,
                    url: 'backup/',
                    success: function(data) {
                        $(".response").show();
                    }
                });
            });
        });

</script>

私のviews.py(コードビハインド)では、リンクを作成してhtmlページに渡します。私は持っている:

def backup(request):
    if request.is_ajax():
        if request.method=='POST':
            //create a link that user can download a file from it. (link)
            variables = RequestContext(request,{'link':link})
            return render_to_response('backup.html',variables)
        else:
            return render_to_response('backup.html')
    else:
        return render_to_response("show.html", {
            'str': "bad Request! :(",
            }, context_instance=RequestContext(request))
backup = login_required(backup)

私の問題: ビューが実行されないようです。このページに送信したリンクが表示されません。jQuery関数のみが実行されているようです。よくわかりません。両方を実行するにはどうすればよいですか (つまり、jQuery 関数と、ビューを実行するこの関数に設定した URL を意味します)。

シリアライズ機能の使い方がわかりません。私が検索するたびに、彼らはそれを書きました:

.serialize() メソッドは、標準の URL エンコード表記でテキスト文字列を作成し、"a=1&b=2&c=3&d=4&e=5.

request.Post[「フィールド名」]でフォームフィールドにアクセスできますが、いつ使用する必要があるかわかりません。そして、成功しているデータが何であるかわかりません:私の状況では関数(データ)です。

どうもありがとうございました。

4

1 に答える 1

1

dataajax ポスト関数からを取得して表示する必要があります。ここでdataは、DJango サーバーを介してレンダリングする応答を示します。次に例を示します。

t = Template("{{ link }}")
c = Context({"link": link})
t.render(c):

JS / jQuery は次のようになります。

<script type="text/javascript">
    $(function() {
        $('#sendButton').click(function(e) {            
            e.preventDefault();
            var temp = $("#backupSubmit").serialize();
            validateForm();
            $.ajax({
                type: "POST",
                data: temp,
                url: 'backup/',

                success: function(data) {
                    // 'data' is the response from your server
                    // (=the link you want to generate from the server)

                    // Append the resulting link 'data' to your DIV '.response'
                    $(".response").html('<p>you can download it<a href="'+data+'">here</a></p>');

                    $(".response").show();
                }
            });
        });
    });
</script>

お役に立てれば。

于 2012-09-16T05:48:52.267 に答える