After I submit a form with an error with django, all fields of the form are cleaned. I want to keep the information on the fields, because it will help the users when they are submiting data.
在我提交带有django错误的表单后,表单的所有字段都会被清除。我想保留字段上的信息,因为它可以帮助用户提交数据。
This is my views.py of the 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)
2 个解决方案
#1
0
I suggest you to use ajax .Because in that we can write different cases to handle if the submission is successful or not. if successful input.val('') else display error and not clean input field .
我建议你使用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
}
});
#2
0
Like Bear Brown said, the data keep on the fields after an error, but how I wasn't using the pure forms from Django, I need to do some adjustments. I created a hidden div with the origial Django forms and passed the data for my fields using JavaScript. This is an example how I proceed:
就像Bear Brown说的那样,数据在出错后仍保留在字段上,但是我没有使用Django的纯表单,我需要做一些调整。我使用原始Django表单创建了一个隐藏的div,并使用JavaScript传递了我的字段的数据。这是我继续的一个例子:
The original Django forms has the id based on the field name on forms. So, if you define the name of the field on forms.py like "name", the id of the field will be "id_name":
原始的Django表单具有基于表单上的字段名称的id。因此,如果您在forms.py上定义“name”字段的名称,则该字段的ID将为“id_name”:
function error(){ document.getElementById('name').value = document.getElementById('id_name').value;}
This is how the fields on form are called. After it's render, it will contains the data of the form field and have an id, so I get the element by id ("id_name") and tranfer the information for my personalizated field.
这就是调用表单上的字段的方式。在渲染之后,它将包含表单字段的数据并具有id,因此我通过id(“id_name”)获取元素并传输我的个性化字段的信息。
<div style="display: none;">
{{ form.name }}
</div>
This is the field with my stylization where the user will edit the data and make his own modifications.
这是我的样式化字段,用户将编辑数据并进行自己的修改。
<input id="name" class="form-control" type="text" maxlength="100" name="name" placeholder="Ex.: Metemática" onchange="slugDefine()" onkeyup="slugDefine()" /><br>
Thank you for your help!
感谢您的帮助!
#1
0
I suggest you to use ajax .Because in that we can write different cases to handle if the submission is successful or not. if successful input.val('') else display error and not clean input field .
我建议你使用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
}
});
#2
0
Like Bear Brown said, the data keep on the fields after an error, but how I wasn't using the pure forms from Django, I need to do some adjustments. I created a hidden div with the origial Django forms and passed the data for my fields using JavaScript. This is an example how I proceed:
就像Bear Brown说的那样,数据在出错后仍保留在字段上,但是我没有使用Django的纯表单,我需要做一些调整。我使用原始Django表单创建了一个隐藏的div,并使用JavaScript传递了我的字段的数据。这是我继续的一个例子:
The original Django forms has the id based on the field name on forms. So, if you define the name of the field on forms.py like "name", the id of the field will be "id_name":
原始的Django表单具有基于表单上的字段名称的id。因此,如果您在forms.py上定义“name”字段的名称,则该字段的ID将为“id_name”:
function error(){ document.getElementById('name').value = document.getElementById('id_name').value;}
This is how the fields on form are called. After it's render, it will contains the data of the form field and have an id, so I get the element by id ("id_name") and tranfer the information for my personalizated field.
这就是调用表单上的字段的方式。在渲染之后,它将包含表单字段的数据并具有id,因此我通过id(“id_name”)获取元素并传输我的个性化字段的信息。
<div style="display: none;">
{{ form.name }}
</div>
This is the field with my stylization where the user will edit the data and make his own modifications.
这是我的样式化字段,用户将编辑数据并进行自己的修改。
<input id="name" class="form-control" type="text" maxlength="100" name="name" placeholder="Ex.: Metemática" onchange="slugDefine()" onkeyup="slugDefine()" /><br>
Thank you for your help!
感谢您的帮助!