【问题标题】:Data driven drop down menu Django数据驱动下拉菜单 Django
【发布时间】:2019-08-02 08:20:06
【问题描述】:

我正在使用数据库“selectCountry”,我想选择数据库中的所有国家/地区名称以填充到 html 下拉菜单中。

以下是我的代码

   models.py

          class selectCountry(models.Model):
                 selectCountryoption = models.CharField(max_length = 30)


views.py
        def loadCountries(request):
         countries = selectCountry.objects.all()
         return render(request, 'register/bg-pages.html', {'countries': 
         countries})

html代码

           <form action=".">
            <select name="Country" id="searchOption">
                <option value="">---------</option>
                {% for country in countries %}
                <option value="{{ countries.selectCountryoption }}">{{ 
                   countries.selectCountryoption }}</option>
                {% endfor %}
            </select>
            <input type="submit" value="submit" name="submit"
         </form>
    </p>

问题是当我请求页面时,列表中没有填充值。

【问题讨论】:

标签: django database


【解决方案1】:

您在 for 循环中使用了错误的值。

<option value="{{ countries.selectCountryoption }}">{{ 
               countries.selectCountryoption }}</option>

应该是

<option value="{{ country.selectCountryoption }}">{{ 
               country.selectCountryoption }}</option>

【讨论】:

  • 不,它仍然无法正常工作。我更改了值并按照您的说明使用
猜你喜欢
  • 1970-01-01
  • 2012-07-01
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 2020-03-29
  • 2020-05-26
相关资源
最近更新 更多