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. A demo site showcasing an example configuration various features, is available in the repository.

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({
                setup: function (element) {
                    // This function is called for each modal that is opened.
                    // You can add any additional setup code here.
                    ...
                }
            });
        });
    </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. To override the footer buttons, you can use the self.footer.clear() method to remove the default buttons and then add your own.

    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, ModalConfirmView, 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 PollEditView(ModalUpdateView):
        model = Poll
        form_class = PollForm
        
        def get_delete_url(self):
            # If you want to show a delete button in the form, you can return the URL for the delete confirmation view.
            return reverse('polls:poll-delete', kwargs={'pk': self.object.pk})
    
    
    class PollConfirView(ModalConfirmView):
        model = Poll
        
        def get_context_data(self):
            context = super().get_context_data()
            context['title'] = 'Close Poll'
            context['message'] = 'Are you sure you want to close this poll?'
            return context
    
        def confirmed(self, *args, **kwargs):
            # Logic to close the poll
            self.object.close()  # Assuming the Poll model has a close method
            return super().confirmed()
      
    
    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>
    

Note:

The data-modal-url attribute should contain the url of the view that will render the modal. It doesn't have to return a form. Non-form modal content can be rendered by overriding the modal_content block in the modal template crisp_modals/modal.html. The following blocks are available for overriding: modal_header, modal_body, modal_footer, modal_scripts. The modal_scripts block should be used to include any additional JavaScript required for the modal content per-instance. Any code needed for all modals should go in the initModal setup option.

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.6.1.tar.gz (12.8 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.6.1-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_crisp_modals-2025.6.1.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.3 Linux/6.14.9-300.fc42.x86_64

File hashes

Hashes for django_crisp_modals-2025.6.1.tar.gz
Algorithm Hash digest
SHA256 e2015f398f29cb2f8f27b3afb49091d7d034fa8987475468902059ca761aeff5
MD5 3b704ef6d75ed2f6ee54b28906f20da4
BLAKE2b-256 5c0009fb67c1a453b8576aee2a95b8ba86a3a3b756642fba67c70192f8669db3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_crisp_modals-2025.6.1-py3-none-any.whl
  • Upload date:
  • Size: 16.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.3 Linux/6.14.9-300.fc42.x86_64

File hashes

Hashes for django_crisp_modals-2025.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c5f9b2432664ecf15d039c86ca53e5cd14eceab55e2ccc173a9cd375416fd3ee
MD5 5f9630915d5d93bc78db5e75d3f12e84
BLAKE2b-256 17cb91ee4850524828f2256c5f0faec26577abe9df0f5aed6bb33227843d8c3a

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