class MultiOriginSelect (forms . SelectMultiple):
class Media :
css = {
'all' : ( '/media/css/fcbkinput.css' ,)
}
js = ( '/media/js/jquery.fcbkcomplete.js' )
class SubscriptionFilterForm (forms . Form):
CHOICES = []
........
orgs = forms . MultipleChoiceField(widget = MultiOriginSelect, choices = CHOICES, required = False , initial = [])
dsts = forms . MultipleChoiceField(widget = MultiOriginSelect, choices = CHOICES, required = False , initial = [])
........
def __init__ ( self , * args, ** kwargs):
super (SubscriptionFilterForm, self ) . __init__( * args, ** kwargs)
fcbkcomplete_fields = [ u'orgs' , u'dsts' ]
for field in fcbkcomplete_fields:
# check whether we have init parameters
if args:
loc_list = args[ 0 ] . getlist(field)
.....
# generate dynamic choices for fcbk fields from args, like [id, name]
self . fields[field] . choices = ([( int (o), name(o)) for o in loc_list] )
<head>
<script>
$( document ).ready( function (){
$( "#id_orgs, #id_dsts" ).fcbkcomplete({
json_url : '/subscribe_autocomplete' ,
first_selected : false ,
filter_hide : true ,
filter_case : false ,
complete_text : "Enter country, city or airport." ,
maxitems : 100
});
});
</script>
</head>
<body>
....
<form>
......
{{form.orgs}}
{{form.dsts}}
<input type = "submit" >
</form>
......
</body>
.....
url(r'^subscribe_autocomplete', subscribe_autocomplete, name='subscribe_autocomplete'),
.....
def subscribe_autocomplete (request):
q = request . GET . get( 'tag' , '' )
# skip too short requests
if len (q) <3 : return HttpResponse( '' )
# filter any instances according to tag
qr = Objects . objects . filter(Q( .... ))
#generate json
#message format - [{"caption":"London", "value":4}]
s =[...];
return HttpResponse(s)
def subs_form_from_model (s):
src_d = {}
src_d[ 'subscriptionemail' ] = s . email
,,,,
qd = django . http . QueryDict( '' )
qd = qd . copy() # to make muttable
qd . update(src_d)
# fill form fields
qd . setlist( 'orgs' , [ unicode (o . id) for o in s . orgs . all()])
qd . setlist( 'dsts' , [ unicode (d . id) for d in s . dsts . all()])
# create form
form = SubscriptionFilterForm(qd)
..........
return form </ code >
Most users see the field already filled (by geo ip) and understand what's what.
The same form can (and should) be used in the admin panel, for large meni-tu-meni fields.
PS And in order not to get up two times - if you liked the buruki (or you are already using our search and mailing list) - we will be happy to communicate with a skilled typesetter and acrobat JSa.
Source: https://habr.com/ru/post/107372/
All Articles