Skip to main content

Django app to implement Bootstrap nested modals

Project description

Add to installed apps in settings
'bootstrap_modals',

Add to template
<script src="{% static 'django_modals/js/modals.js' %}"></script> <link rel="stylesheet" type="text/css" href="{% static 'django_modals/css/modals.css' %}"/>

Sample form and view
from crispy_forms.layout import Layout, Field   
from django_modals.forms import ModelCrispyForm  
from django_modals.view_mixins import BootstrapModelModalMixin   
from .models import User

class UserForm(ModelCrispyForm):
    class Meta:
        model = User
        fields = ['username']
        modal_title = ['Add user', 'Edit User']

    def post_init(self, *args, **kwargs):
        self.helper.layout = Layout(
            Field('username'),
            self.submit_button(css_class="btn-success"),
            self.cancel_button())

class ModalUserForm(BootstrapModelModalMixin):
    form_class = UserForm
Add modal URLS
    path('modal/user/<slug:slug>/', modals.ModalUserForm.as_view(), name='usermodal')
Generate javascript link in python
href_modal('usermodal', 'client-{}'.format(self.kwargs['pk']))
onclick_modal('usermodal', 'client-{}'.format(self.kwargs['pk']))
Adding different buttons

self.button('Delete', [{'function': 'post_modal', 'button_name': 'delete'}], "btn-danger")

in view the function button_buttonname will be called

def button_delete(self, request, *args, **kwargs):

The kwargs from the url are detemined in bootstrap_modals.views in split_slug

if only one item split by -  
    'pk': item
else
    'initial { even : odd }'                     

WIDGETS

Select2

In form

self.fields['field_name'].widget = Select2()`

AJAX

self.fields['field_name'].widget = Select2(attrs={'ajax': True})       

Then in view

def select2_field_name(self, request, *args, **kwargs):
    try:
        results = list(EventSubType.objects.filter(event_type_id=request.POST['field_name'])
                   .values('id').annotate(text=F('name'), value=F('id')))
    except ValueError:
        results = []
    return HttpResponse(json.dumps({'results': results}), content_type='application/json')     

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-nested-modals-0.0.1.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

django_nested_modals-0.0.1-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file django-nested-modals-0.0.1.tar.gz.

File metadata

  • Download URL: django-nested-modals-0.0.1.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.3

File hashes

Hashes for django-nested-modals-0.0.1.tar.gz
Algorithm Hash digest
SHA256 cc6e6cc8238bcf1357bc2a18b34573db3269c0fe235d3a335577179be9cd587c
MD5 87460dac507a939d2c06816fab2fe35a
BLAKE2b-256 367af12239d0f8c1a38eec3d748f6bcb24c7ae997c98677396c682cba1f185cb

See more details on using hashes here.

File details

Details for the file django_nested_modals-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_nested_modals-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.8.3

File hashes

Hashes for django_nested_modals-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea4659cdfb73c20872b92e41359ea2b8e7638ef5e289e0c21e02484a007a636e
MD5 ca9f2c2fcc2c7b5c7c2bdbea7e3abc12
BLAKE2b-256 3466f0b7f365b55d4879b2e90a57382be78b197eb95bf717b59a4de0b7dbbfaf

See more details on using hashes here.

Supported by

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