Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a dynamic search form in Django
    primarykey
    data
    text
    <p>in my Django 1.5 model I'm trying to create a form that let me book a place in a study room.</p> <p>The study room is defined as follows in <code>models.py</code>:</p> <pre><code>class StudyRoom(models.Model): name = models.CharField(max_length = 30, primary_key = True) city = models.CharField(max_length = 30) placesno = models.IntegerField() def __unicode__(self): return self.name </code></pre> <p>and this is the relative form:</p> <pre><code>class SearchSeat(forms.Form): morning = 'AM' evening = 'PM' daysection_choices = ((morning, 'Morning'), (evening, 'Evening'),) city = forms.ChoiceField(choices = [], required=True, label='Select a city?') study_room = forms.ChoiceField(choices = [], required=True, label='Select a study room?') day = forms.DateField(label = 'Select a day', required=True, widget=forms.extras.SelectDateWidget(years=range(2014, 2015))) section = forms.ChoiceField(choices=daysection_choices, label = 'Morning (form 8.00 to 13.00) or evening (from 13.00 to 18..)?') def __init__(self, *args, **kwargs): super(SearchSeat, self).__init__(*args, **kwargs) self.fields['city'].choices = StudyRoom.objects.all().values_list("city","city").distinct() search_city = self.fields['city'] self.fields['study_room'].choices = StudyRoom.objects.filter(city = search_city).values_list("name") </code></pre> <p>The objective was to let the user select the city and then filter the study room and show only the ones in the selected city, all in one form without changing page.</p> <p>The code written like this doesn't work and I'm starting to think that there isn't a solution without using client side side scripting (it's a Django+Python project so we are supposed to use only those two instruments).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload