django: drop down menu not displaying on template despite correct syntax

291 views
Skip to first unread message

clarkso...@gmail.com

unread,
Feb 16, 2016, 8:16:17 AM2/16/16
to Django users

I have successfully configured a way to upload files to a website that I am making. However we would like to include a drop-down menu on the site. However, we want to include in the form of a drop-down menu called "mismatch choice":


models.py: for drop-down menu

             class Mismatches(models.Model):

          MISMATCH_CHOICES = (

           ('0', '0'),

           ('1', '1'),

           ('2', '2'),

           ('3', '3'),

           )

          mismatch = models.IntegerField(max_length=1, default='0', choices = MISMATCH_CHOICES)


forms.py for drop-down menu:

              class MismatchesForm(ModelForm): #unsure how to reference a class in our in-app-urls.py

            class Meta:

        model = Mismatches

        fields = ['mismatch']

'model=Mismatches' links to the Mismatches class in models and the 'field' gives the options.


views.py for drop-down menu

                 class Mismatch_Choice(FormView):

                   template_name = 'list.html'

                   form_class = MismatchesForm



"template_name = 'list.html'" links the html page called list.html. 'form_class' links to the form 'MismatchesForm'.

html for drop-down menu

                <form action="" method="post">{% csrf_token %}

                  {{ form.mismatch }}

                  <input type="submit" value="Submit" />

                </form>


We used a tutorial as a template for our code but it won't display on our html page even though we have referenced it with {{ form.mismatch}} which links to the variable 'mismatch' which is set in our form as the 'model' and hence links to the options given in models.py. 

We are wondering if the html page is not seeing the drop-down menu because it's set as a class in the forms.py but we haven't referenced a class in our in-app urls.py (is there a way to do this?)...

NOTE: all migrations were accordingly applied and the server was restarted multiple times

jorr...@gmail.com

unread,
Feb 17, 2016, 9:22:16 AM2/17/16
to Django users
Have you tried using just {{ form }} instead of {{ form.mismatch }} ?

Also, (not sure if this is related) in your MISMATCH_CHOICES tuple you are giving strings instead of integers for the key value. Try using (0, '0') instead of ('0','0')
Reply all
Reply to author
Forward
0 new messages