2017-07-29 162 views
0

在我用django提交一個帶有錯誤的表單之後,表單的所有字段都會被清除。我想保留這些字段的信息,因爲它會在用戶提交數據時幫助用戶。在django上提交表單並提交錯誤後,保留字段的數據

這是我的aplication的views.py:

def new(request): 
    context = {} 
    if request.method == 'POST': 
     form = NewSubject(request.POST) 
     if form.is_valid(): 
      context['is_valid'] = True 
      form.save() 
      form = NewSubject() 
     else: 
      context['is_valid'] = False 
    else: 
     form = NewSubject() 

    context['form'] = form 
    return render(request, 'subjects/new.html', context) 
+0

在將它發送到服務器之前,在客戶端使用JavaScript進行驗證可能會更好 – AK47

+0

在代碼中,如果表單數據無效,表單數據未清理,可能會在模板上執行某些操作。 –

+0

AK45,我還沒有嘗試使用JavaScript進行驗證,但是如果我可以在服務器上這樣做,對我來說會更有趣,因爲我有一個額外的驗證,就是使用slug。 Bear Brown,我對模板做了一些修改,我沒有使用原始的Django表單,而是使用我自己的程式化的個性化表單。 –

回答

0

像熊布朗說,該數據保持上的錯誤後場,但我怎麼沒有使用純粹的形式從Django的,我需要做一些調整。 我創建了一個帶有原始Django表單的隱藏div,並使用JavaScript爲我的字段傳遞了數據。 這是我如何進行的示例:

原始Django表單具有基於表單上字段名稱的id。所以,如果你定義的,如「名」 forms.py的字段名,字段的ID將是「ID_NAME」:

function error(){ document.getElementById('name').value = document.getElementById('id_name').value;} 

這是如何在表單中的字段被稱爲。渲染完成後,它將包含表單字段的數據並擁有一個id,因此我通過id(「id_name」)獲取元素並傳輸我的個性化字段的信息。

<div style="display: none;"> 
    {{ form.name }} 
</div> 

這是我的風格化,其中用戶將編輯的數據,並做出了自己的改進領域。

<input id="name" class="form-control" type="text" maxlength="100" name="name" placeholder="Ex.: Metemática" onchange="slugDefine()" onkeyup="slugDefine()" /><br> 

謝謝您的幫助!

+0

您可以在'forms.py'中爲輸入添加樣式,如下所示:'name = forms.CharField(label ='Disciplina',widget = forms.TextInput(attrs = {'class':'form-control' ,'placeholder':'Disciplina','onchange':'slugDefine()','onkeyup':'slugDefine()'}))' –

0

我建議你使用Ajax。因爲在我們可以寫不同的情況來處理,如果提交成功與否。如果成功input.val('')else 顯示錯誤並且不乾淨輸入字段。

$('#post-form').on('submit', function(event){ 
event.preventDefault(); 
console.log("form submitted!") // sanity check 
create_post();)}; function create_post() { 
console.log("create post is working!") 
$.ajax({ 
    url : "/lrequestConfirmed/", // the endpoint 
    type : "POST", // http method 
    data : { 
     datef: $('#datepicker').val(), 
     type: $('#type').val(), 
     reason: $('#reason').val() 

    }, // data sent with the post request 

    // handle a successful response 
    success : function(json) { 
     $('#datepicker').val(''); // remove the value from the input 
     $('#reason').val(''); 
     $('#results').html("<div class='alert alert-success alert-dismissable'><a href='#'' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>Success!</strong> Your request has been recored</div>"); 
     console.log(json); // log the returned json to the console 
     console.log("success"); // another sanity check 
    }, 

    // handle a non-successful response 
    error : function(xhr,errmsg,err) { 
     $('#results').html("<div class='alert alert-danger alert-dismissable'><a href='#'' class='close' data-dismiss='alert' aria-label='close'>×</a><strong>Oops!</strong> Something went wrong</div>"); // add the error to the dom 
     console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console 
    } 
});