Can't render django-countries-plus options in form template

337 views Asked by At

For some reason I can't get the list of Countries in django-countries-plus to render in my template.

The table is imported fine under Countries Plus > Countries and I've set the models.py up as follows:

from countries_plus.models import Country

class Project(models.Model):
    title = models.CharField(max_length=500)
    status = models.BooleanField(default=True)
    location = models.CharField(max_length=500)
    projectcountry = models.ForeignKey(Country)

Then added the new field projectcountry to the form on forms.py:

from django.forms import ModelForm
from .models import Project, Proposal

class ProjectForm(ModelForm):

class Meta:
    model = Project
    fields = ['title', 'status', 'location', 'projectcountry']

Then in the template I'm trying to bring in the 252 countries in the table to render in a Bootstrap dropdown:

<form class="form-horizontal" method="POST" enctype="multipart/form-data">
    {% csrf_token %}

    <div class="form-group">
      <label class="col-sm-2 control-label">PROJECT TITLE</label>
      <div class="col-sm-6">
        <textarea rows="1" class="form-control" name="title"></textarea>
      </div>
    </div>

    <div class="form-group">
      <label class="col-sm-2 control-label">STATUS</label>
      <div class="col-sm-6">
        <select name="status" class="form-control">
          <option value="1">Active</option>
          <option value="0">Disabled</option>
        </select>
      </div>
      <div class="col-sm-4">
      </div>
    </div>

    <div class="form-group">
      <label class="col-sm-2 control-label">CITY / TOWN</label>
      <div class="col-sm-6">
        <textarea rows="1" class="form-control" name="location"></textarea>
      </div>
    </div>

    <div class="form-group">
      <label class="col-sm-2 control-label">COUNTRY</label>
      <div class="col-sm-6">
        <select name="projectcountry" class="form-control">
          {% for country in countries %}
            <option value="{{ country.iso }}">{{ country.name }}</option>
          {% endfor %}
        </select>
      </div>
      <div class="col-sm-4">
      </div>
    </div>
</form>

Can you see what I'm doing wrong? I've been struggling with this researching all day but not cracked it.

0

There are 0 answers