I’m really new to django and I’m building a form and I need a select box for the user to select their country. I’m playing with the ChoiceField and I created a separate py file with a list of countries. my layout is something like this:
form file:
from django import COUNTRIES #I have a py file with the list of countries
country = forms.ChoiceField(COUNTRIES, label=u’Country’)
I kinda didn’t expect it to work – I got an error saying that it cannot import name countries . I don’t know what step to take to achieve my goal. Any helpful tips?
you can import modules the same way you would in a non django python module.
The module you made with the list of countries is not a django module, so you don’t import it from django.
If you had a file, my_choices.py which looked like this:
extras.py
and it is located in your project’s root dir:
if your project is in the PYTHONPATH, you can import the choices by typing:
i usually put stuff like this in a utils folder:
so:
in your forms.py