0

ダイアログ ウィンドウからフォームを送信し、同じダイアログ ウィンドウに応答を表示しようとしています。現在、(検証エラーを含む) フォームを html-string として返す Django ビューを試しています。

template = "register.html"
html = render_to_string(template, {'form': form})
return HttpResponse(html, content_type="text/html; charset=utf-8") 

しかし、ダイアログモーダルの機能に問題があります。「モーダル/ダイアログ」のコンテンツを HttpResponse の新しい html に置き換える部分がありません。

$('#registerform').submit(function(e){
e.preventDefault();
$.post('register', function(data){ 

//somthing is missing here..             
});
return false;    
});

フォーマットについては今のところわかりませんが、アイデアはわかります。専門家が私を正しい方向に導くことができれば、私は幸せな男になるでしょう! ありがとう

4

1 に答える 1

0

これは Django ブレースを使用した実装です: https://github.com/brack3t/django-braces

//Inside your click, event whatever call...
...
// Get the token initialy, this is the way to do it 
// using the jQuery cookie plugin 
var $csrftoken = $.cookie('csrftoken');
$.ajax({
    type: 'POST',
    url: '/your/post/url/',
    crossDomain: false,
    beforeSend: function(xhr) {
        xhr.setRequestHeader("X-CSRFToken", $csrftoken);
    },
    success: function(ctx) {
        """
        ctx contains the data your view returns, 
        If you are using Django braces, you could implement the 
        AjaxResponseMixin mixin which allows you to return a dict 
        to the view.
        """
    },
});
...

ビュー.py

class AjaxDosomethingView(JSONResponseMixin, AjaxResponseMixin, View):

    def post_ajax(self, request, *args, **kwargs):
        """
        Do some stuff and create the response object
        """
        myid = request.POST.get('somevar')
        result = MyModel.objects.get(id=myid)
        response = {'id': result.id, 'title': result.title}

        return self.render_json_response(response, 200)
于 2014-01-21T21:27:04.237 に答える