0
我準備使用脆皮從我的django投影,但在測試中,發生了一個問題,我不知道發生了什麼錯誤。所以我在這裏發佈代碼。 如果有人能指出這個錯誤,我會很感激。 形式:我在django中使用香脆形式的工作,第一次點擊提交按鈕後呈現失敗,第二次點擊提交按鈕沒有響應
class TestForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(),label='Name',max_length=100,)
def __init__(self,*args,**kwargs):
super(TestForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Button('save', 'save'))
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-3'
self.helper.field_class = 'col-lg-8'
self.helper.form_id = 'pkg-form'
class Meta:
model = PkgList
fields = (
'id','name',
)
urls.py:
url(r'^testform/$',test_form,name='test_form')
在我的代碼的觀點:
@json_view
def test_form(request):
if request.method == 'POST'and request.is_ajax():
form = TestForm(request.POST)
if form.is_valid():
return {'success': True}
else:
form_html = render_crispy_form(form)
return {'success': False, 'form_html': form_html}
else:
if request.method == 'GET':
form = TestForm()
return render(request,'man/pkg_form.html',{'form':form})
templetes:
<div class="modal-dialog" xmlns="http://www.w3.org/1999/html">
<div class="modal-content message_align">
<div class="modal-body" align="center">
<span class="section">Pkg Info</span>
{% crispy form form.helper%}
</div>
<div>
</div>
$('#button-id-save').on('click', function(event){
event.preventDefault();
var form = '#pkg-form';
$.ajax({
url: "{% url 'pkg_view' %}",
type: "POST",
data: $(form).serialize(),
success: function(data) {
if (!(data['success'])) {
$(form).replaceWith(data['form_html']);
}else {
window.location.href = "{%url 'man/pkglist' %}";
}
},
error: function() {
$(form).find('.error-message').show()
}
});
return false;
});
第一AJAX POST和返回{ '成功':假, 'form_html':form_html}後,從後臺,點擊按鈕無法發送AJAX POST沒有更多.....
謝謝各位大大的幫助.. –