R
R
R.2017-09-02 23:06:15
JavaScript
R., 2017-09-02 23:06:15

Why is an empty set of data added to the database when the page is opened?

When the page is opened normally, for some reason a post request is sent and json with null values ​​is added to the database.
Where did the bug creep in and what's wrong?
Thank you!

views.py

def add_car(request):
    """ Lets user add car card """
    if request.POST:
        data = request.POST
        car_dict = dict()

        car_dict['model'] = data.get('model')
        car_dict['brand'] = data.get('brand')
        car_dict['year'] = data.get('year')
        car_dict['vin'] = data.get('vin')

        json_data = json.dumps(car_dict)
        MyCar.objects.create(car=json_data)
    return render(request, 'car_adding_form.html', locals())


script.js

$(document).ready(function(){

    $("#add_car").click(function(e){
        e.preventDefault();
        var extraForm = document.createElement('DIV');
        $(extraForm).attr("data-is-sent", 0);
            
           ...
           ...
           ...

            $.ajax({
            type: "POST",
            data: data,
            cache: true,
            });
            $("[data-is-sent='0']").attr("data-is-sent", 1);
        });
    });


null json

{"vin": null, "brand": null, "year": null, "model": null}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2017-09-02
@Astrohas

if request.method == "POST":
add a method check in the view

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question