1

出于某种原因,我无法将 django-countries-plus 中的国家列表呈现在我的模板中。

该表在国家加号 > 国家下可以正常导入,我将 models.py 设置如下:

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)

然后将新字段 projectcountry 添加到 forms.py 的表单中:

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

class ProjectForm(ModelForm):

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

然后在模板中,我尝试在表中引入 252 个国家/地区以在 Bootstrap 下拉列表中呈现:

<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>

你能看出我做错了什么吗?我整天都在为这项研究而苦苦挣扎,但没有破解它。

4

0 回答 0