gpt4 book ai didi

python - Django Forms 无法呈现单选按钮

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:33 25 4
gpt4 key购买 nike

抱歉,我对 Django 很陌生。我读了很多答案,但无法使其发挥作用。我错过了一些东西

这是我的 forms.py

    from django import forms
from .models import Post
from django.utils.translation import gettext as _



class EventsForm(forms.ModelForm):
class Meta:
CHOICES=[('select1','select 1'), ('select2','select 2')]
model = Post
widgets = {
'title': forms.TextInput(attrs={'placeholder': _('form-title')}),
'type_id': forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect),
}
fields = ('title', 'type_id', 'post_date', )

我想要type_id的单选按钮

我收到错误

'ChoiceField' object has no attribute 'attrs'

你能解释一下我做错了什么吗:)

谢谢

最佳答案

这对我有用!

    class EventsForm(forms.ModelForm):
CHOICES=(('select1','select 1'), ('select2','select 2'))
type_id = forms.TypedChoiceField(choices=CHOICES, widget=forms.RadioSelect)
class Meta:
model = Post
fields = ('title', 'type_id', 'post_date', )

关于python - Django Forms 无法呈现单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37633882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com