How to make dropdown with <options> tag in html from choices in django model?

Viewed 17

I have authentication app with

models:

LIVING_COUNTRIES = [
    ('AFGANISTAN', 'Afganistan'),
    ('ALBANIA', 'Albania'),
    ('ALGERIA', 'Algeria'),
    ('ANGORRA', 'Andorra'),
    ('ANGOLA', 'Angola')]

class Employee(models.Model):
    first_name          =               models.CharField(max_length=100, blank=True)
    last_name           =               models.CharField(max_length=100, blank=True)
    username            =               models.CharField(max_length=30, blank=True)
    email               =               models.EmailField(max_length=140, blank=True)
    # phone_number        =               PhoneNumberField(null=True)
    date_of_birth       =               models.DateField(blank=True, default='1929-22-22')
    education           =               models.CharField(max_length=50, blank=True)
    country_living      =               models.CharField(max_length=50, choices=LIVING_COUNTRIES, default='UNITEDSTATESOFAMERICA', blank=True)
    created_at          =               models.DateTimeField(auto_now_add=True, blank=True)
    password            =               models.CharField(max_length=30, null=True)

Now I want to display country_living field in my html form.

I have tried like this:

<select name="category" id="id_category">
  {% for category in living_countries.country_living %}
      <option value="{{ category.country_living }}">{{ category.country_living }</option>
  {% endfor %}
</select>
def get(self, request):

        context = {}
        living_countries = models.Employee.objects.all()
        context['living_countries'] = living_countries

        return render(request, 'authentication/employee_register.html', context)

But it doesn't work. Does anyone know how to access and display this field?

1 Answers

LIVING_COUNTRIES is a list in your models.py file but it is outside the Employee model, so there's no way for it to show up inside your view and from your view to your template.

to resolve that in your views, you can do from from .models import * this way the model and list will be available to us.

Better way of working would be to have a model for LivingCountries and use that as one-to-one relationship with Employee model.

Next, in views, you need to correct the query from models.Employee.objects.all() to Employee.objects.all()

the view function can be simplified as following and the list should be passed within the function as following

def get(request):
    employees = Employee.objects.all()
    living_countries_list = LIVING_COUNTRIES
    return render(request, 'authentication/employee_register.html', {
        'employees': employees,
        'living_countries_list': living_countries_list
    })

since now you have employee data and living countries list being sent to template, you can do following to get list of countries

in your template

<select name="category" id="id_category">
  {% for each in living_countries_list %}
      <option value="{{ each.0 }}">{{ each.1 }</option>
  {% endfor %}
</select>
Related