rx-36

How to create a simple modal window form with Django and JqueryUI and communicate via Ajax Part 1

This article demonstrates how to use modal windows in Django with jQuery UI.

Although jQuery was once indispensable for web development with Django, it has rapidly declined in popularity with the rise of various front-end frameworks. However, when using Django's default templating engine for front-end development, jQuery still offers a simple and straightforward way to solve certain problems without added complexity. In fact, numerous tutorials combining Django and jQuery still exist online, and many remain practical and useful. For many Django developers, jQuery remains a valued option for its simplicity.

JQueryUI

jQuery has a long history with UI components, most notably through jQuery UI. While some newer developers might dismiss jQuery UI as outdated, it remains functional and well-documented. Moreover, it integrates seamlessly with Django templates as a UI component library.

jQuery UI is one of the simplest options for implementing modal windows in a Django front-end.

Application specifications

Backend- Django

Frondend- Django's default template, jquery, and jqueryUI.

AJAX is used for communication between the front-end and back-end.

A Note on Terminology

Note that jQueryUI uses the term "dialog," but "modal" is a more common term, so we will use it throughout this article.

Project Overview

Django Development Environment

I won't cover the basic Django configuration, as it's covered extensively in other tutorials.

Additionally, please refer to this article for information on setting up Django with Docker.

If you follow this tutorial based on my article (Basic setup for running Django with Docker), pause at the step where you create a new application using the startapp command (Section 8).

Install Django Packages

Development Process

  1. Creating a New Application
  2. Define a model in models.py
  3. Register the Defined Model in admin.py
  4. Creating a Template
  5. Creating forms.py
  6. Creating a basic views.py
  7. Adding new application to urls.py of Project
  8. Adding a View to urls.py on the Application
  9. Adding Basic jQueryUI Code to the Template
  10. Implement AJAX to enable data communication between the view and the jQueryUI component
  11. Applying Data Validation Within the View
  12. Displaying a Success Message upon Successful Data Storage

1. Creating a New Application

2. Define a model in models.py

3. Register the Defined Model in admin.py

4. Creating a Template and static folders

5. Creating forms.py

6. Creating a basic views.py

7. Adding new application to urls.py of Project

8. Adding a View to urls.py on the Application

9. Adding Basic jQueryUI Code to the Template

10. Implement AJAX to enable data communication between the view and the jQueryUI component

11. Applying Data Validation Within the View

12. Displaying a Success Message upon Successful Data Storage

Changing CSS

The default design looks a bit outdated. Let's adapt the CSS to match this blog's design to show that customizing jQueryUI is easy.

I'll cover CSS customization in Part 2, as this article is already quite long.

GitHub Repository

The complete project is available on GitHub in the jqueryui_modal repository; I hope it proves useful in your work.