django-国家/地区下拉列表不显示

Dee*_*end 2 python django

这是这个问题的第三次迭代,因为错误已经解决(在一些人的感激帮助下)。为了避免对到底发生了什么感到困惑,我认为有必要重新发布更新的详细信息。

我正在使用 Django 1.6.4。

我正在尝试将django-countries 应用程序与 Django 一起使用,但未显示下拉列表。我没有收到任何错误,但下面的 Survey.html 页面未显示 ISO 3166-1 国家/地区列表的预期下拉列表。

我通过 pip 在项目的虚拟环境中安装了 django-countries 2.1.2。它已添加到已安装的应用程序中

已安装的应用程序

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    'survey',
    'django_countries',
)
Run Code Online (Sandbox Code Playgroud)

模型.py

from django.db import models
from django_countries.fields import CountryField

class Person(models.Model):
    country = CountryField()

    def __unicode__(self):
        return self.country 
Run Code Online (Sandbox Code Playgroud)

视图.py

from django.shortcuts import render    
from django.db import models
from django_countries.fields import CountryField
from models import SexChoice, AgeChoice, RelationshipStatusChoice, Person

def survey(request):

    age = AgeChoice()
    sex = SexChoice()
    relationship = RelationshipStatusChoice()   
    country = Person()

    return render(request, 'survey.html', {
                                           'age': age,
                                           'sex': sex,
                                           'relationship': relationship,     
                                           'country': country,                                      
                                           })
Run Code Online (Sandbox Code Playgroud)

调查.html

<html> 
    <body>

        <h1>Experiment Survey</h1>

            <form action="" method="post">
                {% csrf_token %}
                <h3>What age are you?</h3>
                    {{age.as_p}}

                <h3>What sex are you?</h3>
                    {{sex.as_p}}

                <h3>What is your current relationship status?</h3>
                    {{relationship.as_p}}

                <h3>What country are you from?</h3>

                    {{country.as_p}}

                <input type="submit" value="Submit" />               
            </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我以为这会让我失望,country.as_p但我什么也没看到。我没有收到任何错误。

提前致谢。

Max*_*ant 5

根据文档,模块中有一个 2 元组的元组可用于填充您的字段:

从 Python 获取国家/地区

使用django_countries.countries对象实例作为 ISO 3166-1 国家/地区代码和名称(按名称排序)的迭代器。

所以以下应该有效:

from django.db import models
from django_countries.fields import CountryField
from django_countries import countries

class Person(models.Model):
    country = CountryField(choices=list(countries))

    def __unicode__(self):
        return self.country
Run Code Online (Sandbox Code Playgroud)

编辑:经过讨论,我因读操作代码的速度太快而完全混淆了。事实上,您需要创建一个Form而不是直接在模板中使用模型:

class SurveyForm(forms.Form):
    age = forms.CharField()
    sex = forms.CharField()
    relationship = forms.CharField()
    country = forms.CountryField(choices=list(countries))

#####

def survey(request):
    form = SurveyForm()

    return render(request, 'survey.html', {'form': form})


#####

My whole form:
{{ form.as_p }}
Run Code Online (Sandbox Code Playgroud)

正如我在聊天中所说,文档中提供了进一步的说明