Django Ajax PUT Request Example Tutorial

Published On: 22/08/2022 | Category: Django

Hi Dev,

This article will provide example of django ajax put request example. you will learn how do i put with jquery/ajax in django. In this article, we will implement a jquery ajax put json response example. This tutorial will give you simple example of how do i get data from my ajax put to my django view. So, let's follow few step to create example of django ajax patch request example tutorial.

When you click on the edit button then we will open the model with username and email input and then we will save data using the ajax put method. you can edit data using jquery ajax get in django all version as well.

Now let's move on to the PUT request. In our current scenario we have to edit users user profile with ajax PUT request.

This should look similar to a POST request. The only difference is the shape of the URL:

Here i explained simply step by step example of django ajax put request example.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2: Create a App
python3 startapp core
Step 3: Update

In this step we require to do add installed apps in our file. Add the below lines to your file:

Next, you need to add it in the file as follows:

Step 4: Creating the Views

In this step, we need to create the views for performing fetch record to the database.Open the core/ file and add:

from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.models import User
from django.http import JsonResponse,HttpResponse
from django.http import HttpResponse
import json

# Create your views here.
def user_list(request):
    users = User.objects.all()
    return render(request, "home.html", {"users": users})

def update_user(request,id):
    if request.method == 'PUT':
        obj = get_object_or_404(User, id=id)
        data = json.load(request)

        username = data.get('username')
        email = data.get('email')


        return JsonResponse({'success':'User Updated Successfully!'})
        return JsonResponse({'errors':'Something went wrong!'})
Step 5: Creating the Templates

Next, open the core/templates/home.html file and the add:

<!DOCTYPE html>
    <title>Django Ajax PUT Request Example  -</title>
    <link href="" rel="stylesheet">
    <script src=""></script>
    <script src="[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<div class="container mt-5 pt-5">
    <h2>Django Ajax PUT Request Example - <span class="text-primary"></span></h2>
    <table class="table table-bordered data-table">
                <th width="120">Action</th>
            {% for user in users %}
                <tr data-id="{{ }}">
                    <td>{{ }}</td>
                    <td>{{ user.username }}</td>
                    <td>{{ }}</td>
                            class="btn btn-primary"
            {% endfor %}
<!-- Modal -->
<div class="modal fade" id="userEditModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Edit User</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            <div class="modal-body">
                <input type="hidden" id="user-id" name="id"></span>
                <p><strong>Name:</strong> <br/> <input type="text" name="username" id="user-name" class="form-control"></span></p>
                <p><strong>Email:</strong> <br/> <input type="email" name="email" id="user-email" class="form-control"></span></p>
            <div class="modal-footer">
                <button type="button" class="btn btn-success" id="user-update">Update</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<script type="text/javascript">
    $(document).ready(function () {
        When click user on Edit Button
        $('body').on('click', '#edit-user', function () {
        When click user on Show Button
        $('body').on('click', '#user-update', function () {
            var id = $('#user-id').val();
            var username = $('#user-name').val();
            var email = $('#user-email').val();
                url: '/users/update/' + id,
                type: 'PUT',
                dataType: 'json',
                data: JSON.stringify({username: username, email: email,}),
                headers: {
                    "X-CSRFTOKEN": "{{ csrf_token }}"
                success: function(data) {
Step 6: Creating URLs

In this section, we’ll create the urls to access our CRUD views.Go to the core/ file and update it as follows:

from django.urls import path
from core import views

urlpatterns = [
    path('users/', views.user_list, name="user_list"),
    path('users/update/<int:id>', views.update_user, name="update_user"),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/users/ address with a web browser.

I Hope It will help you....