Sign Up Form

Sign Up

Display data into modal form through django ?

720 421 point-admin
  • 0

To display data into a modal form through Django, you need to integrate Django with JavaScript and a frontend framework like Bootstrap for the modal. Here’s how to do it:

Step 1: Set Up Your Django View

Create a view to fetch and return the data.

# views.py
from django.shortcuts import render, get_object_or_404
from .models import YourModel
from django.http import JsonResponse

def get_data(request, pk):
    data = get_object_or_404(YourModel, pk=pk)
    return JsonResponse({'field1': data.field1, 'field2': data.field2})

Step 2: Create the URL Pattern

Add a URL pattern for the view.

# urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('get-data/<int:pk>/', views.get_data, name='get_data'),
]

Step 3: HTML for the Modal

Create a modal in your template.

<!-- template.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Modal with Data</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
    <!-- Button to Open the Modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" onclick="loadData(1)">
        Open Modal
    </button>

    <!-- The Modal -->
    <div class="modal" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">
                <!-- Modal Header -->
                <div class="modal-header">
                    <h4 class="modal-title">Modal Heading</h4>
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                </div>

                <!-- Modal body -->
                <div class="modal-body">
                    <p id="field1"></p>
                    <p id="field2"></p>
                </div>

                <!-- Modal footer -->
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <script>
        function loadData(pk) {
            $.ajax({
                url: `/get-data/${pk}/`,
                method: 'GET',
                success: function (data) {
                    $('#field1').text(data.field1);
                    $('#field2').text(data.field2);
                }
            });
        }
    </script>
</body>
</html>

Explanation:

  1. Django View: The get_data view fetches data from the model and returns it as JSON.
  2. URL Pattern: The URL pattern matches the view to an endpoint.
  3. HTML: The modal is defined using Bootstrap, and a button triggers the modal and loads data.
  4. JavaScript: The loadData function uses jQuery to make an AJAX request to fetch data and populate the modal fields.

When the button is clicked, it triggers the loadData function, which fetches data from the server and updates the modal content dynamically. Make sure to replace YourModel and field names with your actual model and field names.

Leave a Reply

Your email address will not be published.