Django - How to call localflavor US_STATES in HTML

170 Views Asked by At

How do you make a drop down menu that contains all of the options in localflavor's US_STATES?

I can see how to create a model that contains a field that uses the localflavor option US_STATES.

class State(models.Model):
    states           = models.CharField(max_length=2, choices=US_STATES  , null=True, blank=True) 

The field state is then in a manytomany relationship to a model called Person. How do you take this a put it in a html page?

In my view, I can only think of doing this.

def get_context_data(self, *args, **kwargs):
    context = super(UserProfileUpdateView, self).get_context_data(*args, **kwargs)
    context['states'] = State.objects.all()

But this only pulls the existing state options.

1) How do I pull all states into the view?

2) How can I render an html template to use the output of 1? I imagine it has something to do with the 'choices' option, but I haven't ever done that before.

Thanks

1

There are 1 best solutions below

0
On

You probably want to take a look at Django's forms system. Since what you're doing here is rendering a form (I'm guessing that by "drop down menu" you mean an HTML <select> element with all the states as options), that would be the preferred way to do it.

The localflavor package includes form field classes for working with its data types, including states.