【问题标题】:Django how to make select formDjango如何制作选择表单
【发布时间】:2020-08-01 06:41:36
【问题描述】:

我想在用户注册时选择电话服务机构。但我的选择表单没有出现在 html 中。 有没有办法制作选择表格?

models.py

        class User(AbstractUser):
    ...
            phone_service_provider = models.CharField(
                max_length=11, choices=AGENCY_CHOICES, blank=False
            )
    ...

forms.py:

    class SignUpGeneralForm(forms.Form):
...
        address_postcode_general = forms.CharField(
            widget=forms.TextInput(
                attrs={
                    "id": "postcode",
                    "placeholder": "우편번호",
                    "class": "appearance-none bg-transparent border-none w-full text-gray-700 mr-3 py-1 px-2 leading-tight focus:outline-none",
                    "style": "width:100%; height:100%;",
                    "readonly": "true",
                }
            )
        )
    
        phone_service_provider = forms.ChoiceField(
            widget=forms.Select(choices=AGENCY_CHOICES)
        )
    
        phone_number = forms.CharField(
            widget=forms.TextInput(
                attrs={
                    "placeholder": "핸드폰 번호",
                    "class": "appearance-none bg-transparent border-none w-full text-gray-700 mr-3 py-1 px-2 leading-tight focus:outline-none",
                    "style": "width:100%; height:100%;",
                }
            )
        )
...

【问题讨论】:

    标签: django django-models django-forms


    【解决方案1】:

    formsField中的widget只用于html属性,选择需要在Field中,没有widget,所以:

       phone_service_provider = forms.ChoiceField(
            widget=forms.Select(),
            choices=AGENCY_CHOICES
        )
    

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 2015-11-08
      • 2016-02-22
      • 1970-01-01
      • 2020-04-27
      • 2013-11-11
      • 2016-08-17
      • 2023-03-10
      • 2012-11-24
      相关资源
      最近更新 更多