2013-02-28 51 views
6

Tôi đang gặp sự cố khi gửi biểu mẫu bằng Ajax. Tôi có một biểu mẫu mà tôi muốn thay thế bằng một biểu mẫu khác một cách không đồng bộ khi nhấp vào nút tiếp theo.yêu cầu django ajax

Đây là kịch bản

$(document).ready(function() { 
    $('#MY_FORM').submit(function() { 
     $.ajax({ 
      data: $(this).serialize(), 
      type: $(this).attr('method'), 
      url: $(this).attr('action'), 
      success: function(response) { 
       $('#FORM_DIV').html(response); 
      } 
     }); 
     return false; 
    }); 
}); 

form.py

class CountyForm(forms.Form): 
    county = forms.ModelChoiceField(queryset=County.objects.all(), 
       empty_label='---Select a county---', required=False) 
    other = forms.CharField(required=False) 

    def __init__(self, *args, **kwargs): 
     super(CountyForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper(self) 
     self.helper.html5_required = True 
     self.helper.form_id = 'MY_FORM' 
     self.helper.add_input(Submit('next', 'Next', css_class='classfinish')) 
     self.helper.layout = Layout('county','other') 


class WardForm(forms.Form): 
    ward = forms.ModelChoiceField(queryset=Ward.objects.all(), 
      empty_label='Select a ward') 
    other = forms.CharField() 

    def __init__(self, *args, **kwargs): 
     super(WardForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper(self) 
     self.helper.html5_required = True 
     self.helper.add_input(Submit('save', 'Finish')) 
     self.helper.add_input(Submit('cancel', 'Cancel')) 
     self.helper.layout = Layout('ward','other') 

xem

def location(request): 
    if request.is_ajax() : 
     wardform = WardForm() 
     return HttpResponse(wardform) 
    countyform = CountyForm() 
    c = {} 
    c.update(csrf(request)) 
    return render(request,'location.html', {'countyform': countyform}) 

tôi muốn khi tôi bấm nút bên cạnh dưới dạng hạt, dạng phường xuất hiện .

+3

Câu hỏi của bạn là gì? Bạn đang thiếu một trích dẫn sau '# FORM_DIV'. –

+0

JS của bạn có lỗi cú pháp – Blender

+1

@Pavel là lỗi đánh máy, cảm ơn. – Alexxio

Trả lời

1

Điều này có thể được giải quyết theo hai cách, tôi sẽ không đề cập đến FormWizard nhưng tôi sẽ cung cấp cho bạn liên kết đến tài liệu thực sự tốt.

Đề nghị của tôi là bạn nên đi cho một FormWizard (tùy thuộc vào phiên bản của Django bạn có) nhưng tôi nghĩ rằng nó đã được di chuyển vào Django 1,2 và trở đi nhưng không báo cho tôi về điều đó.

Trong tình huống khó khăn hiện tại, bạn nên làm điều gì đó dọc theo các dòng này.

$(document).ready(function() { 
    $('#MY_FORM').submit(function() { 
     $.ajax({ 
      data: $(this).serialize(), 
      type: $(this).attr('method'), 
      url: $(this).attr('action'), 
      success: function(response) { 
       $('#FORM_DIV').load('/some/url/to/a/wardform'); //new code 
      } 
     }); 
     return false; 
    }); 
}); 

views.py

def ward_form_renderer(request): 
    if request.is_ajax(): 
     ward_form = WardForm() 
     #depending on version of django 
     context = {'wardform': ward_form} 
     context.update(csrf(request)) 
     render(request, 'wardform_template', c) 

Điều này sẽ làm là nó sẽ kéo một trang html mới với một hình thức trả khỏi tầm nhìn Django của bạn và hiển thị nó. Về cơ bản những gì bạn đã làm sau đó là FormWizard. Cách tiếp cận này sẽ có tác dụng phụ khác vì vậy tôi sẽ không khuyên bạn nên làm theo cách này.

Bản thân tôi đang ở trong một dự án thực hiện điều này và đau đớn hơn là thành thật. Tôi đã không thử mã này bản thân mình nhưng bạn sẽ có được ý chính về cách thức hoạt động của nó.

Nhập FormWizard! Đây là lựa chọn tốt đặc biệt cho bạn vì bạn không phải xác định lại các biểu mẫu của mình, bạn có thể sử dụng các biểu mẫu mà bạn có.

Here's the link to the FormWizard docs

Chúc may mắn!