如何解决如何获得这些性别选择以单选按钮显示
我是Django的新手,我不知道如何在模型中选择性别,然后选择模板。 以下是我的Models.py:
os.chdir()
下面是我的Forms.py:
from django.db import models
from django.forms import ModelForm
from model_utils import Choices
class Country(models.Model):
''' define a table in the database called Country with several variables.'''
name = models.CharField(max_length = 50,unique = True)
officiallang = models.CharField(max_length = 50)
size = models.IntegerField()
# TODO: Find out how to defined this relationship
# representative = models.OneToOneField(UNrepresentative)
class UNrepresentative(models.Model):
GENDER_CHOICES = (
('M','Male'),('F','Female'),('O','Other'),)
name = models.CharField(max_length = 100,unique = True)
gender = models.CharField(max_length = 1,choices = GENDER_CHOICES,)
country = models.OneToOneField(Country,on_delete = models.CASCADE,)
class CountryForm(ModelForm):
class Meta:
model = Country
fields = ['name','officiallang','size']
class UNrepresentativeForm(ModelForm):
class Meta:
model = UNrepresentative
fields = ['name','gender','country']
最后,这是create_representative.html,其中包含模板代码:
from django import forms
from app1.models import Country,UNrepresentative
class CountryForm(forms.Form):
name = forms.CharField(max_length = 50,)
officiallang = forms.CharField(max_length = 50)
size = forms.IntegerField()
class UNrepresentativeForm(forms.Form):
name = forms.CharField(max_length = 100,)
gender = forms.ChoiceField(
choices = UNrepresentative.GENDER_CHOICES,)
我已经手动添加了男性和女性的值,但理想情况下,我希望拥有一个带有单选按钮的下拉菜单以选择一种性别。任何帮助将不胜感激。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。