Django-Select2 Documentation. Nirupam Biswas

Similar documents
Bambu API Documentation

Django Map Widgets Documentation

django-subdomains Documentation

django-antispam Documentation

Django Map Widgets Documentation

django-osm-field Release 0.3.1

tally Documentation Release Dan Watson

Easy-select2 Documentation

django-autocomplete-light Documentation

django-embed-video Documentation

django-mongonaut Documentation

django-conduit Documentation

django-embed-video Documentation

Graphene Documentation

django-avatar Documentation

django-avatar Documentation

Django Leaflet Documentation

django-autocomplete-light Documentation

Django Extra Views Documentation

spaste Documentation Release 1.0 Ben Webster

django-cms-search Documentation

django-sticky-uploads Documentation

django-password-reset Documentation

django-openid Documentation

django-debreach Documentation

Bitdock. Release 0.1.0

django-ratelimit-backend Documentation

Django Groups Manager Documentation

Google Domain Shared Contacts Client Documentation

django-facebook-graph Documentation

HOW TO MAKE A FULL FLEDGED REST API. with DJANGO OAUTH TOOLKIT

bottle-rest Release 0.5.0

PyZabbixObj Documentation

Quick housekeeping Last Two Homeworks Extra Credit for demoing project prototypes Reminder about Project Deadlines/specifics Class on April 12th Resul

maya-cmds-help Documentation

How to Add Categories and Subcategories to WordPress

Django-frontend-notification Documentation

Django Better Cache Documentation

django-session-security Documentation

django-baton Documentation

Django PAM Documentation

Django Admin Sortable Documentation

Django Forme Documentation

Magento Survey Extension User Guide

Django REST Framework JSON API Documentation

django-embed-video Documentation

Bricks Documentation. Release 1.0. Germano Guerrini

django-scaffold Documentation

Django File Picker Documentation

django-renderit Documentation

django-baton Documentation

django-fluent-dashboard Documentation

certbot-dns-route53 Documentation

ComSi WooCommerce product synchronization between unlimited webshops

Stepic Plugins Documentation

Django Groups Manager Documentation

Gargoyle Documentation

django-oauth2-provider Documentation

Python AutoTask Web Services Documentation

Django IPRestrict Documentation

Django File Picker Documentation

Jquery Manually Set Checkbox Checked Or Not

django-dajax Documentation

Custom Actions for argparse Documentation

django-reinhardt Documentation

Django Synctool Documentation

Django-CSP Documentation

Django Extras Documentation

Django Image Tools Documentation

django-sendgrid-events Documentation

django-intercom Documentation

webkitpony Documentation

Django-Front Documentation

Course Details. Skills Gained. Who Can Benefit. Prerequisites. View Online URL:

staff Documentation Release 1.2

RiotWatcher Documentation

django-audit-log Documentation

Flask restful swagger Documentation

Django. Jinja2. Aymeric Augustin DjangoCong 2016

Django QR Code Documentation

django-gollum Documentation

SagePay payment gateway package for django-oscar Documentation

Tailor Documentation. Release 0.1. Derek Stegelman, Garrett Pennington, and Jon Faustman

pytest-benchmark Release 2.5.0

Kiki Documentation. Release 0.7a1. Stephen Burrows

django-model-utils Documentation

micawber Documentation

Django eztables Documentation

wagtail-robots Documentation

djangotribune Documentation

fragapy Documentation

colab Documentation Release 2.0dev Sergio Oliveira

django-modern-rpc Documentation

nidm Documentation Release 1.0 NIDASH Working Group

django-image-cropping Documentation

Django EL(Endless) Pagination Documentation

PySpec Documentation. Release Zac Stewart

django-dajaxice Documentation

Ensure Documentation. Release Andrey Kislyuk

CIS192 Python Programming

Transcription:

Nirupam Biswas Mar 07, 2018

Contents 1 Get Started 3 1.1 Overview................................................. 3 1.2 Installation................................................ 3 1.3 External Dependencies.......................................... 3 1.4 Example Application........................................... 3 2 API Documentation 5 2.1 Configuration............................................... 5 2.2 Widgets.................................................. 5 2.3 URLs................................................... 5 2.4 Views................................................... 5 2.5 Cache................................................... 5 2.6 JavaScript................................................. 5 2.7 Security & Authentication........................................ 6 3 Extra 7 3.1 Chained select2.............................................. 7 3.2 Interdependent select2.......................................... 8 3.3 Multi-dependent select2......................................... 8 4 Indices and tables 11 Python Module Index 13 i

ii

Contents: Contents 1

2 Contents

CHAPTER 1 Get Started 1.1 Overview This is a Django integration of Select2. The application includes Select2 driven Django Widgets and Form Fields. 1.2 Installation 1. Install django_select2: pip install django_select2 2. Add django_select2 to your INSTALLED_APPS in your project settings. 3. Add django_select to your urlconf if you use any ModelWidgets: url(r'^select2/', include('django_select2.urls', 1.3 External Dependencies jquery version 2 This is not included in the package since it is expected that in most scenarios this would already be available. 1.4 Example Application Please see tests/testapp application. This application is used to manually test the functionalities of this package. This also serves as a good example. 3

4 Chapter 1. Get Started

CHAPTER 2 API Documentation 2.1 Configuration 2.2 Widgets 2.3 URLs 2.4 Views 2.5 Cache 2.6 JavaScript DjangoSelect2 handles the initialization of select2 fields automatically. Just include {{ form.media.js }} in your template before the closing body tag. That s it! If you insert forms after page load or if you want to handle the initialization yourself, DjangoSelect2 provides a jquery plugin. It will handle both normal and heavy fields. Simply call djangoselect2(options on your select fields.: $('.django-select2'.djangoselect2(; You can pass see Select2 options if needed: $('.django-select2'.djangoselect2({placeholder: 'Select an option'}; 5

2.7 Security & Authentication Security is important. Therefore make sure to read and understand what the security measures in place and their limitations. Set up a separate cache. If you have a public form that uses a model widget make sure to setup a separate cache database for Select2. An attacker could constantly reload your site and fill up the select2 cache. Having a separate cache allows you to limit the effect to select2 only. You might want to add a secure select2 JSON endpoint for data you don t want to be accessible to the general public. Doing so is easy: class UserSelect2View(LoginRequiredMixin, AutoResponseView: pass class UserSelect2WidgetMixin(object: def init (self, *args, **kwargs: kwargs['data_view'] = 'user-select2-view' super(userselect2widgetmixin, self. init (*args, **kwargs class MySecretWidget(UserSelect2WidgetMixin, Select2ModelWidget: model = MySecretModel search_fields = ['title icontains'] 6 Chapter 2. API Documentation

CHAPTER 3 Extra 3.1 Chained select2 Suppose you have an address form where a user should choose a Country and a City. When the user selects a country we want to show only cities belonging to that country. So the one selector depends on another one. 3.1.1 Models Here are our two models: class Country(models.Model: name = models.charfield(max_length=255 class City(models.Model: name = models.charfield(max_length=255 country = models.foreignkey('country', related_name="cities" 3.1.2 Customizing a Form Lets link two widgets via dependent_fields. class AddressForm(forms.Form: country = forms.modelchoicefield( queryset=country.objects.all(, label=u"country", widget=modelselect2widget( model=country, search_fields=['name icontains'], (continues on next page 7

(continued from previous page city = forms.modelchoicefield( queryset=city.objects.all(, label=u"city", widget=modelselect2widget( model=city, search_fields=['name icontains'], dependent_fields={'country': 'country'}, max_results=500, 3.2 Interdependent select2 Also you may want not to restrict the user to which field should be selected first. Instead you want to suggest to the user options for any select2 depending of his selection in another one. Customize the form in a manner: class AddressForm(forms.Form: country = forms.modelchoicefield( queryset=country.objects.all(, label=u"country", widget=modelselect2widget( search_fields=['name icontains'], dependent_fields={'city': 'cities'}, city = forms.modelchoicefield( queryset=city.objects.all(, label=u"city", widget=modelselect2widget( search_fields=['name icontains'], dependent_fields={'country': 'country'}, max_results=500, Take attention to country s dependent_fields. The value of city is cities because of related name used in a filter condition cities which differs from widget field name city. Caution: Be aware of using interdependent select2 in parent-child relation. When a child is selected, you are restricted to change parent (only one value is available. Probably you should let the user reset the child first to release parent select2. 3.3 Multi-dependent select2 Furthermore you may want to filter options on two or more select2 selections (some code is dropped for clarity: 8 Chapter 3. Extra

class SomeForm(forms.Form: field1 = forms.modelchoicefield( widget=modelselect2widget( field2 = forms.modelchoicefield( widget=modelselect2widget( field3 = forms.modelchoicefield( widget=modelselect2widget( dependent_fields={'field1': 'field1', 'field2': 'field2'}, 3.3. Multi-dependent select2 9

10 Chapter 3. Extra

CHAPTER 4 Indices and tables genindex modindex search 11

12 Chapter 4. Indices and tables

Python Module Index d django_select2, 3 13

14 Python Module Index

Index D django_select2 (module, 3 15