Sign in
Log inSign up

Django +ajax +mongodb integration.

NB's photo
NB
·Sep 19, 2018

Hey everyone,I am begginer in django and implementing a project which has a login page and registration page and once the users will enter details in registration page then it should be stored in MongoDB and on another page I want to display that data in table format so and for that, I am using ajax but it is failing. And another question is do i need to write script for connecting mongodb or " djongo " will work.Please help.

I am using : Django 2.0.7 python 3.7

here's the code register.html

......

<p class="test"></p>
<table id="table" class="hidden" border="1">
    <tr>
        <thead>
        <tr>
            <th>Name</th>
            <th>Mobile Number</th>
            <th>Gender</th>
            <th>Delete</th>
            <th>Edit</th>

        </tr>
        </thead>
        <tbody id="table-body">

        {% for i in result %}
            <tr>
                <td id="na">
                    {#                {{ i.name }}#}
                </td>
                <td>
                    {#                {{ i.Mobile_num }}#}
                </td>
                <td>
                    {#                {{ i.Gender }}#}
                </td>
                <td>
                    {#                <button id='{{ i|mongo_id }}' class='botaoadd' name='delete'>delete</button>#}
                </td>
                <td>
                    {#                <button id='{{ i|mongo_id }}' class='btupdate' name='update'>update</button>#}
                </td>

            </tr>
        {% endfor %}
        </tbody>
</table>


</body>

<script type="text/javascript">
    var obid1;

    $("#submit").click(function (e) {
        var fname = $("#fname").val();
        var mno = $("#mno").val();
        var gen = $('input[name=gender]:checked').val();

        var datas = {
            "Name": fname,
            "Mobile_num": mno,
            "Gender": gen,
        };
        store_table(datas)

        get_my_table()
    });

    $("#table").on("click", ".btupdate", function (e) {
        obid = $(this).attr('id')
        $("#submit").hide()
        $("#edit").show()
        $("#cancel").show()
        obid1 = obid
        var d_update = {
            "oid": obid,
            "action": "get_entry"
        }
        $.ajax({
            url: "/get_entry",
            type: "POST",
            data: d_update,
            success: function (response) {
                $("#fname").val(response[0]["name"])
                $("#mno").val(response[0]["Mobile_num"])

            },
            error: function (response) {
                console.log("This is after clicking edit button")

            }

        })


    })

    $("#cancel").click(function (e) {
        $("#submit").show()
        $("#edit").hide()
        $("#cancel").hide()
        $("#fname").val("")
        $("#mno").val("")

    })

views.py

def update_view(request, *args, **kwargs): #this method updates the entry in database using the object id
    try:

        if request.method == 'POST':
                customer_dict1 = dict()
                request_body = request.POST
                customer_dict1["_id"] = request_body.get("_id")
                customer_dict1["name"] = request_body.get("Name")
                customer_dict1["Mobile_num"] = request_body.get("Mobile_num")
                customer_dict1["Gender"] = request_body.get("Gender")
                result = database.customer_info.update({"_id": ObjectId(customer_dict1["_id"])},
                                              {"$set":{"name":customer_dict1["name"],"Mobile_num":customer_dict1["Mobile_num"],
                                                    "Gender":customer_dict1["Gender"]}})
                return HttpResponse("successfully updated")
                result = get_db()
                print(result)
        return render(request, 'dashboard/register.html', {"result": result})
        return render(request, 'dashboard/register.html', {})
    except Exception as e :
            return HttpResponse("failed")

so in this it showing httpresponse as failed