2012-07-25 41 views
11

Tôi là người mới làm việc với django. Tôi cần ví dụ đơn giản. Làm cách nào để gửi biểu mẫu (bài đăng) mà không cần làm mới trang bằng cách sử dụng Django, Ajax, jQuery?Làm cách nào để gửi biểu mẫu mà không cần làm mới trang bằng cách sử dụng Django, Ajax, jQuery?

Đây là hình thức, quan điểm và mẫu của tôi:

views.py

from django.shortcuts import * 
from django.template import RequestContext 
from linki.forms import * 

def advert(request): 
    if request.method == "POST": 
     form = AdvertForm(request.POST) 

     if(form.is_valid()): 
      print(request.POST['title']) 
      message = request.POST['title'] 

     else: 
      message = 'something wrong!' 


     return render_to_response('contact/advert.html', 
       {'message':message}, 
      context_instance=RequestContext(request)) 

    else: 
     return render_to_response('contact/advert.html', 
       {'form':AdvertForm()}, 
      context_instance=RequestContext(request)) 

forms.py (mẫu sử dụng "ModelForm")

from django import forms 
from django.forms import ModelForm 
from linki.models import Advert 


class AdvertForm(ModelForm): 
    class Meta: 
     model = Advert 

MẪU (dạng mã html)

<html> 
<head> 

</head> 
    <body> 
    <h1>Leave a Suggestion Here</h1> 
     {% if message %} 
      {{ message }} 
     {% endif %} 
     <div> 
      <form action="" method="post">{% csrf_token %} 
       {{ form.as_p }} 
       <input type="submit" value="Submit Feedback" /> 
      </form> 
     </div> 
    </body> 
</html> 

Trả lời

14

nếu bạn đang có kế hoạch để sử dụng một ajax nộp với jquery bạn không nên trở html khỏi tầm nhìn của bạn .. Tôi đề nghị bạn để làm điều này thay vì:

html:

<html> 
<head> 
</head> 
<body> 
    <h1>Leave a Suggestion Here</h1> 
     <div class="message"></div> 
     <div> 
      <form action="" method="post">{% csrf_token %} 
       {{ form.as_p }} 
       <input type="submit" value="Submit Feedback" /> 
      </form> 
     </div> 
</body> 
</html> 

js

$('#form').submit(function(e){ 
    $.post('/url/', $(this).serialize(), function(data){ ... 
     $('.message').html(data.message); 
     // of course you can do something more fancy with your respone 
    }); 
    e.preventDefault(); 
}); 

các views.py

import json 
from django.shortcuts import * 
from django.template import RequestContext 
from linki.forms import * 

def advert(request): 
    if request.method == "POST": 
     form = AdvertForm(request.POST) 

     message = 'something wrong!' 
     if(form.is_valid()): 
      print(request.POST['title']) 
      message = request.POST['title'] 

     return HttpResponse(json.dumps({'message': message})) 

    return render_to_response('contact/advert.html', 
      {'form':AdvertForm()}, RequestContext(request)) 

sao cho bạn đặt câu trả lời trong số message div. thay vì trả về html đơn giản, bạn nên trả về json.

+0

dữ liệu.message trong phần JS phải là JSON.parse (dữ liệu) .message – rawbeans

1
$('#form-id').submit(function(e){ 
    $.post('your/url', $(this).serialize(), function(e){ ... }); 
    e.preventDefault(); 
}); 
6
<script type="text/javascript"> 
$(document).ready(function() { 
    $('#form_id').submit(function() { // catch the form's submit event 
     $.ajax({ // create an AJAX call... 
      data: $(this).serialize(), // get the form data 
      type: $(this).attr('method'), // GET or POST 
      url: $(this).attr('action'), // the file to call 
      success: function(response) { // on success.. 
       $('#success_div).html(response); // update the DIV 
      }, 
      error: function(e, x, r) { // on error.. 
       $('#error_div).html(e); // update the DIV 
      } 
     }); 
     return false; 
    }); 
}); 
</script> 
Các vấn đề liên quan