Skip to main content

Django Crispy Forms inside Bootstrap Modals

Project description

django-crisp-modals

django-crisp-modals is a Django app which provides support for ajax Django Crispy Forms inside Bootstrap 5 modal dialogs. The app provides various views, form classes, templates and javascript.

Quick start

  1. Add "crisp_modals", and "crispy_forms" to your INSTALLED_APPS setting like this::

    INSTALLED_APPS = [
        ...,
        "crispy_forms",
        "crispy_bootstrap5",
        "crisp_modals",
    ]
    
  2. Add the following to your settings.py file::

    CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
    CRISPY_TEMPLATE_PACK = "bootstrap5"
    
  3. Include the crisp_modals/modals.min.js within your base template after jQuery and add a blank modal div to the bottom of the body tag. The modal div should have the id: modal-target. Then initialize the modal target. For example:

    {% load static %}
    ...
    <div id="modal-target"></div>
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/jquery-form@4.3.0/dist/jquery.form.min.js"></script>
    <script src="{% static 'crisp_modals/modals.min.js' %}"></script>
    <script>
        $(document).ready(function() {
            $('#modal-target').initModal();
        });
    </script>
    
  4. Create forms as follows. The main crispy-forms helper is available as self.body within the forms. A footer helper is also available as self.footer, with submit and reset buttons already included.

    from crisp_modals.forms import ModalModelForm, Row, FullWidth
    
     class PollForm(ModalModelForm):
          class Meta:
               model = Poll
               fields = ['question', 'pub_date']
     
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         if self.instance.pk:
             self.body.form_action = reverse('polls:poll-update', kwargs={'pk': self.instance.pk})
         else:
             self.body.form_action = reverse('polls:poll-create')
         self.body.append(
             Row(
                 FullWidth('question', placeholder='Enter your question'),
             ),
             Row(
                 FullWidth('pub_date', placeholder='Enter the publication date'),
             )
         )
    
  5. In your views, use the ModalCreateView, ModalUpdateView, and ModalDeleteView classes as follows. Include delete_url in the form kwargs for the ModalUpdateView class to show the delete button within the form.

    from crisp_modals.views import ModalCreateView, ModalUpdateView, ModalDeleteView
    
    class PollCreateView(ModalCreateView):
        model = Poll
        form_class = PollForm
    
    class PollCreateView(ModalUpdateView):
        model = Poll
        form_class = PollForm
        
        def get_form_kwargs(self):
            kwargs = super().get_form_kwargs()
            kwargs['delete_url'] = reverse('polls:poll-delete', kwargs={'pk': self.object.pk})
            return kwargs
    
    class PollDeleteView(ModalDeleteView):
        model = Poll
    
  6. To distinguish regular links from links that target modals, use the 'data-modal-url' attribute instead of href. For example:

    <a href="#0" data-modal-url="{% url 'polls:poll-create' %}" class="modal-link">Create Poll</a>
    <a href="#0" data-modal-url="{% url 'polls:poll-update' pk=poll.pk %}">Update Poll</a>
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_crisp_modals-2025.3.15.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_crisp_modals-2025.3.15-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file django_crisp_modals-2025.3.15.tar.gz.

File metadata

  • Download URL: django_crisp_modals-2025.3.15.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for django_crisp_modals-2025.3.15.tar.gz
Algorithm Hash digest
SHA256 3bc9313152435ddfa21f7cb447de550cf7f02428b5f3642825e945a2fd93461b
MD5 ece2947ea8efe03b142a3cfacf9d321c
BLAKE2b-256 01d039fd18a2d6ea0847ac481cdbb5e21b93c0ec46cf425b9a5082024a627106

See more details on using hashes here.

File details

Details for the file django_crisp_modals-2025.3.15-py3-none-any.whl.

File metadata

File hashes

Hashes for django_crisp_modals-2025.3.15-py3-none-any.whl
Algorithm Hash digest
SHA256 e76944da0f5dfc3a7ce2d001806427b594ce0ae86a42d83e49fbe957c57966cb
MD5 0e75bd49a8d62700fd6cedc62350d08e
BLAKE2b-256 0a673015aecab5c5b849beaac1d3bc08e9a3b71007bd472d31868e86953fdfe8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page