django-countries 下拉菜单不显示

2 投票
2 回答
2021 浏览
提问于 2025-04-18 06:56

这是这个问题的第三次提问,因为之前的错误已经解决了(多亏了几位朋友的帮助)。为了避免大家对发生了什么感到困惑,我觉得有必要重新发布更新的细节。

我正在使用 Django 1.6.4。

我想在 Django 中使用 django-countries 应用,但是下拉菜单没有显示出来。我没有收到任何错误提示,但下面的 survey.html 页面没有显示出我期待的 ISO 3166-1 国家列表的下拉菜单。

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

INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
    'survey',
    'django_countries',
)

models.py

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

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

    def __unicode__(self):
        return self.country 

views.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,                                      
                                           })

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

我原以为这会给我一个 country.as_p 的下拉菜单,但我什么也没看到。我没有收到任何错误。

谢谢大家的帮助。

2 个回答

3

你需要这个表单:

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

注意,你需要在表单类中使用ChoiceField()。CountryField()是用在模型里的。

5

根据文档,这个模块里有一个包含二元组的元组,可以用来填充你的字段:

从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

编辑:经过讨论,我发现自己因为读得太快而搞混了原作者的代码。实际上,你需要创建一个 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 }}

正如我在聊天中提到的,文档中有更多详细的解释

撰写回答