Django:清空从Ajax发送到Python的数据

时间:2018-06-27 07:19:33

标签: python jquery ajax django

我正在尝试获取从Ajax发送到Python的字符串数据。但是,当我记录代码时,不会发送数据。它是空的。你能告诉我为什么吗?

p.s。该代码的目的是获取提交按钮的名称以在views.py

中进行验证

javascript

<script type='text/javascript'>
$(".submit-btn").click(function(){
  var button_pressed = JSON.stringify($(this).attr('name'));
  $.ajax({
    url: "{% url 'add_test' %}",
    type: 'POST',
    data: {'button_pressed' : button_pressed, 'csrfmiddlewaretoken': $("[name=csrfmiddlewaretoken]").val()},
  })
});
</script>

views.py

def add_test(request):
    if request.method == 'POST':
        test_name = request.POST.get('test_name')
        test_type = request.POST.get('test_type')
        test_date = request.POST.get('test_date')
        test_obj = Test(test_name = test_name, test_type = test_type, test_date = test_date)
        test_obj.save()
        button_pressed = request.POST.get('button_pressed')
        #button_pressed = json.loads(button_pressed)        
        print button_pressed ,"+++++++++++++++++++++++++++++"
        return HttpResponseRedirect('/test-management/test/')

test_list.html

<form class="form" method="POST" action="{% url 'add_test' %}">
                        {% csrf_token %}
                        <h2>Add Test</h2>
                        <div class="card-body">
                            <div class="form-group bmd-form-group">
                                <div class="input-group">
                                    <p>Name :</p>
                                    <input type="text" class="form-control" id="test_name" name="test_name" placeholder="Test Name...">
                                </div>
                            </div>
                            <div class="form-group bmd-form-group">
                              <div class="input-group">
                                  <p>Type :</p>
                                  <input type="text" class="form-control" id="test_type" name="test_type" placeholder="Test Type...">
                              </div>
                            </div>
                            <div class="form-group bmd-form-group">
                                <div class="input-group">
                                    <p>Date :</p>
                                    <input type="date" class="form-control" id="test_date" name="test_date" placeholder="Test Date...">
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer justify-content-right">
                            <button type="submit" class="btn btn-primary btn-link btn-wd btn-lg" data-dismiss="modal">Cancel</button>
                            <button type="submit" class="btn btn-primary btn-link btn-wd btn-lg submit-btn" name="add_new">Add & New</button>
                            <button type="submit" class="btn btn-primary submit-btn" name="add">Add</button>
                        </div>
</form>

我是Django的新手,如果问题很傻,请您抱歉。感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

更改类型submit按钮:-

<button type="submit" class="btn btn-primary submit-btn" name="add">Add</button>

<button type="button" class="btn btn-primary submit-btn" name="add">Add</button>

答案 1 :(得分:0)

我想您的表单是使用“提交”按钮发送的。它发送您的form数据,而不是ajax数据。

只需使用其他按钮发送ajax或添加preventDefault()

<script type='text/javascript'>
$(".submit-btn").click(function(e){
  e.preventDefault();
  var button_pressed = JSON.stringify($(this).attr('name'));
  $.ajax({
    url: "{% url 'add_test' %}",
    type: 'POST',
    data: {'button_pressed' : button_pressed, 'csrfmiddlewaretoken': $("[name=csrfmiddlewaretoken]").val()},
  })
});
</script>

此外,如果要接收表单数据,则必须更改视图

答案 2 :(得分:0)

我发现,如果提交按钮的类型为'submit',我们将无法获得它的名称。因此,我将类型设为'button'。为了使代码正常工作,我做了很多修改,但是现在一切正常。

javascript

<script type='text/javascript'>
$(".submit-btn").click(function(){
  var test_name = JSON.stringify($("#test_name").val());
  var test_type = JSON.stringify($("#test_type").val());
  var test_date = JSON.stringify($("#test_date").val());
  var button_pressed = JSON.stringify($(this).attr('name'));
  $.ajax({
    url: "{% url 'add_test' %}",
    type: 'POST',
    data: {'button_pressed' : button_pressed, 'test_name' : test_name, 'test_type' : test_type,
    'test_date' : test_date,'csrfmiddlewaretoken': $("[name=csrfmiddlewaretoken]").val()},
  })
  var button_pr = $(this).attr('name');
  if (button_pr == 'add_new'){
    $("#test_name").val("");
    $("#test_type").val("");
    $("#test_date").val("");
  } else {
    $(".cancel-btn").click();
  }
});
</script>

views.py

def add_test(request):
    if request.method == 'POST':
        test_name = json.loads(request.POST.get('test_name'))
        test_type = json.loads(request.POST.get('test_type'))
        test_date = json.loads(request.POST.get('test_date'))
        test_date = datetime.strptime(test_date, '%Y-%m-%d').date()
        test_obj = Test(test_name = test_name, test_type = test_type, test_date = test_date)
        test_obj.save()
        return HttpResponseRedirect('/test-management/test/')

test_list.html

<form class="form" method="POST" action="{% url 'add_test' %}">
                        {% csrf_token %}
                        <h2>Add Test</h2>
                        <div class="card-body">
                            <div class="form-group bmd-form-group">
                                <div class="input-group">
                                    <p>Name :</p>
                                    <input type="text" class="form-control" id="test_name" name="test_name" placeholder="Test Name...">
                                </div>
                            </div>
                            <div class="form-group bmd-form-group">
                              <div class="input-group">
                                  <p>Type :</p>
                                  <input type="text" class="form-control" id="test_type" name="test_type" placeholder="Test Type...">
                              </div>
                            </div>
                            <div class="form-group bmd-form-group">
                                <div class="input-group">
                                    <p>Date :</p>
                                    <input type="date" class="form-control" id="test_date" name="test_date" placeholder="Test Date...">
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer justify-content-right">
                            <button type="button" class="btn btn-primary btn-link btn-wd btn-lg cancel-btn" data-dismiss="modal">Cancel</button>
                            <button type="button" class="btn btn-primary btn-link btn-wd btn-lg submit-btn" name="add_new">Add & New</button>
                            <button type="button" class="btn btn-primary submit-btn" name="add">Add</button>
                        </div>
                    </form>

基本上,我编写Java脚本代码来获取表单字段的值,并将其作为Ajax发送到'add_test'。要获取我单击的按钮的名称,我无法使用类型'submit'的按钮。谢谢