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': '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
Release history Release notifications | RSS feed
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.2.tar.gz
(22.2 kB
view hashes)
Built Distribution
Close
Hashes for django-nested-modals-0.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a31e22e730dbbd9592c3656bb314e18b65e04cce772ff905c3357008ff81e5b7 |
|
MD5 | ce184b8ba1216ef2a447bb8a7a0c2907 |
|
BLAKE2b-256 | 46f49e9307cf7fbc2023db49a6628331c990159a2eb572dd499cdf0a4cb993ea |
Close
Hashes for django_nested_modals-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c291fa074aaa9700779a11eddffa00409752e226abecfc157491e28b4a515726 |
|
MD5 | bad33cf58f0b7303276f885f7b81e2a7 |
|
BLAKE2b-256 | a47bcef4612078adb7db2825805fa54c4f46f6675ef885e37ce8917d8719343b |