Start a team blog
Start a team blog, invite your team, and start publishing.

Django +ajax +mongodb integration.

NB's photo
·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">
            <th>Mobile Number</th>

        <tbody id="table-body">

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

        {% endfor %}


<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,


    $("#table").on("click", ".btupdate", function (e) {
        obid = $(this).attr('id')
        obid1 = obid
        var d_update = {
            "oid": obid,
            "action": "get_entry"
            url: "/get_entry",
            type: "POST",
            data: d_update,
            success: function (response) {

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




    $("#cancel").click(function (e) {



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

        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"])},
                return HttpResponse("successfully updated")
                result = get_db()
        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