如何在注册表中使用 django 国家?

How to use django countries in registration form?

我正在尝试在将与 django-allauth 一起使用的注册表中添加 django-countries。按照说明 https://github.com/SmileyChris/django-countries 我创建了一个模型

class UserProfile(models.Model):
    # Other things
    country = CountryField()

还有一个表格

从django_countries.data进口国家

class SignupForm(forms.Form):
    # Other stuff
    country = forms.ChoiceField(choices=COUNTRIES, required=True)

    def signup(self, request, user):
        # Other Stuff
        user.userprofile.country = self.cleaned_data['country']

但是当我访问 /accounts/signup/ 页面时,我得到了表格,但是对于国家 select 我得到了

<p><label for="id_country">Country:</label> <select id="id_country" name="country">
<option value="G">Q</option>
<option value="I">D</option>
<option value="K">Y</option>
 ...

代替国家代码和国家名称

您应该这样设置选项:

from django_countries import countries
COUNTRY_CHOICES = tuple(countries)

class SignupForm(forms.Form):
    country = forms.ChoiceField(choices=COUNTRY_CHOICES, required=True)