django dropdownlist onchange submission
22,223
Solution 1
solved following this link
days = forms.ModelChoiceField(queryset=Day.objects.all().order_by('alias'), widget=forms.Select(attrs={"onChange":'refresh()'}))
Solution 2
Find the id
of the dropdown using firebug. It should be id_days
as you are using the name days
. Then bind jQuery change
event to it.
$(function(){
$('#id_days').change(function(){
$('#id_of_form').submit();
});
});
Author by
toni
Updated on July 20, 2022Comments
-
toni almost 2 years
I am showing to the user a dropdown list from this form:
class CronForm(forms.Form): days = forms.ModelChoiceField(queryset=Day.objects.all().order_by('day')) class Day(models.Model): day = models.CharField(max_length=200, default='') month = models.CharField(max_length=200, default='') def __unicode__(self): return self.day
And It is shown at the template this way:
<form method="post" onchange=change()> {{ days }} </form>
What I want is to submit this form when the user selects an option from the dropdown list. For example, the user could be redirected to a new url, and internally the program would capture the POST data. But everything I find is related to the
<select>
tag, like calling a javascript function onchange of the select element. But as the select element is IN the ModelChoiceField form, I don't know how to do it. Any help would be very appreciated! -
toni about 11 yearsand if I only want to filter the objects that are shown? I have tried
queryset=Day.objects.get(Q(day="15")).order_by('alias')
,queryset=Day.objects.filter(day="15").order_by('alias')
and many other options to no avail