2012-02-25 17 views
1

djangoでajaxを使用するためのヒントとヒントを得るために、Django、jquery、およびmodelforms

def add_comment(request, pk): 
    if request.method == 'POST' and request.is_ajax(): 
    comment_form = CommentForm(request.POST) 
    if comment_form.is_valid(): 
     comment = comment_form.save(commit=True) 
     comment.save() 
    json = simplejson.dumps(comment, ensure_ascii=False) 
    return HttpResponse(json, mimetype='application/json') 
    return render_to_response({{ post.id }}', {'comment': comment,}), context_instance=RequestContext(request), mimetype='application/json') 

と私はAjaxの機能を持つリダイレクトせずにページにコメントを投稿しようとしている:私は私がアップミキシングだと信じてい

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script></javascript> 
<script type="text/javascript"> 
    $(document).click(function() 
    { 
    $('#comment_form').submit(function() 
    { 
    var dataString = $('#comment_form').serialize(); 
    $.ajax({ 
     type: 'POST', 
     url: '', 
     data: dataString, 
     success: function(data){ 
     $('').html(data); 
     }, 
    }); 
    return false; 
    }); 
    }); 

</script> 

は、私が機能を持っていると言いますここにはいくつかのものがあります。私はリダイレクトせずにコメントを読み込むページを取得しようとしています。私は正確な答えを必要としません、ちょうど正しい方向に操縦されるかもしれません。

+0

あなたの質問がありますか? – Yann

+0

私はDjangoでajaxを使ってコメントを投稿するための洞察とヒントを得るように頼んだ。それはポストの最上部にある、それは明確ではなかった? – tijko

+0

あなたはdjangoとajaxに精通していますが、私はあなたのプロフィールを見ていて、matplotで質問に答えるかのように思えます。あなたはいくつかの洞察力を持っている場合は、共有してください。 – tijko

答えて

0

感謝。 jqueryが主な問題でした。

$(document).ready(function() { 
    $('#comment_form').submit(function(e) { 
    e.preventDefault(); 
    $.ajax({ 
    type: 'POST', 
    url: '{% url art.views.post %}', 
    data: $('#comment_form').serialize(), 
    dataType: 'json'; 
    success: function(){ 
     location.reload(); 
$('#comment_form').get(0).reset(); 
    }, 
    }); 
    return false; 
    }); 
}); 

実際のフォームデータではなくDOMオブジェクトをビューに送信していました。

私は2つの機能を組み合わせて2つのURLを共有するようにしました。

def post(request, pk): 
    post = Post.objects.get.(pk=int(pk)) 
    comments = Comment.objects.filter(post=post) 
    _dict = dict(post=post, comments=comments, form=Comment_form(), user=request.user) 
    _dict.update(csrf(request)) 
    cf_obj = Comment(post = Post.objects.get(pk=pk)) 
    if request.method == 'POST' and request.is_ajax(): 
    if comment_form.is_valid(): 
     comment = comment_form.save(commit=True) 
    else: 
     raise Http404 
    response = serializers.serialize('json', [comment]) 
    return HttpResponse(response, mimetype='application/json') 
    return render_to_response('post.html', d) 
2

この缶に役立ちます:

これはあなたのビューのようになります。

import json 

def add_comment(request, pk): 
    if request.method == 'POST' and request.is_ajax(): 
     comment_form = CommentForm(request.POST) 
     if comment_form.is_valid(): 
      comment = comment_form.save(commit=True) 
      comment.save() 
      json_response = json.dumps({"status":"Success"}) 
      return HttpResponse(json_response) 
     errors = {} 
     for k, v in job_type_form.errors.items(): 
      errors[k.capitalize()] = v 
     response = { 
      'success': False, 
      'errors': errors 
     } 
     return HttpResponse(json.dumps(response)) 

とあなたのjqueryのは、このようなことができます:私は最終的に物事が働いてしまったの記事を

$('#comment_form').submit(function() { 
    var dataString = $('#comment_form').serialize(); 
    $.ajax({ 
     type: 'POST', 
     url: '',// you need to put this to something like '{% url to_your_view %}' 
     data: dataString, 
     dataType: 'json' 
     success: function(data){ 
      // you can access to your json object like data.status or data.something 
      $('').html(data.status); 
     }, 
    }); 
    return false; 
}); 
+0

お返事いただきありがとうございます。私はこのコードを試してみるつもりです。表示するURLを扱っているajax関数に関するコメントは、{{post.id}}に入れていましたが、これはうまくいきませんでした。私はリダイレクトせずにページにコメントを投稿しようとしています。私は成功のページに私を返す多くの情報を見つけました、私はすでに投稿するコメントを得ることができましたが、私はメインページへのリダイレクトだけです。 – tijko

+0

これを試してみたら、それでもうまくいきませんでした。私はすべてのものをまた行きます。 – tijko

+0

私はそこにもいくつかの構文エラーがあると信じています。 – tijko

関連する問題