保留django表单中提交错误后的字段数据

2024-09-27 21:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

在我用django提交一个有错误的表单之后,表单的所有字段都将被清除。我想保留字段上的信息,因为它将帮助用户提交数据。在

这是我的视图.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)

Tags: djangoform信息表单newifisrequest
2条回答

就像bearbrown所说的,数据在出错后仍然保留在字段上,但是我怎么没有使用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":

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.

^{pr2}$

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>

谢谢你的帮助!在

我建议您使用ajax,因为这样我们可以编写不同的案例来处理提交是否成功。如果成功输入值.val('')其他 显示错误和未清除输入字段。在

$('#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
    }
});

相关问题 更多 >

    热门问题