gpt4 book ai didi

python - FormName 没有属性 'error_messages'

转载 作者:行者123 更新时间:2023-12-01 06:26:46 27 4
gpt4 key购买 nike

我请求你帮助我克服这个错误:“NewAnnouncementForm”没有属性“error_messages”

所以,我有一个“公告”模型:

class Announcement(models.Model):

announcement = 'announcement'
alert = 'alert'
offer ='offer'

announcement_types = (
(announcement, 'Announcement'),
(alert, 'Alert'),
(offer, 'Offer'),
)

id = models.AutoField(auto_created=True, primary_key=True, serialize=True, verbose_name='ID')
text = models.CharField(max_length=1500, null=False, blank=False)
announcement_type = models.CharField(max_length=12, choices=announcement_types, default=announcement)
user_name = models.ForeignKey(CustomUserModel, db_column='user_name', on_delete=models.CASCADE)
date = models.DateTimeField(auto_now=True)
price = models.DecimalField(max_digits=6, decimal_places=2)
building_id = models.ForeignKey(Buildings, db_column='building_id', on_delete=models.CASCADE)

我正在尝试制作一个表单和 View 以允许用户添加他的公告。这是我的表格:

class NewAnnouncementForm(forms.ModelForm):

text = forms.CharField(label='Treść ogłoszenia', max_length=1500)
announcement_type = forms.ChoiceField(label='Typ ogłoszenia', widget=forms.RadioSelect, choices=Announcement.announcement_types)
#user_name dodac do views przed savem i dodac w save (?)
price = forms.DecimalField(label='Podaj oczekiwaną cenę, w przypadku darmowego ogłoszenia wpisz 0')
user_name = forms.CharField(widget=forms.HiddenInput())
building_id = forms.IntegerField(widget=forms.HiddenInput())

class Meta(forms.Form):
model = Announcement
fields = ('text', 'announcement_type', 'price', 'user_name', 'building_id')

def save(self, commit=True):
announcement = super(NewAnnouncementForm, self).save(commit=False)
announcement.text = self.cleaned_data['text']
announcement.announcement_type = self.cleaned_data['announcement_type']
announcement.price = self.cleaned_data['price']

if commit:
announcement.save()
return announcement

我认为views.py是一个主要问题:

def add_announcement(request):
if request.user.is_authenticated:
if request.method=='POST':
username = str(request.user.username)
user_object = CustomUserModel.objects.values_list('address', flat=True).get(username=username)
form = NewAnnouncementForm(request.POST)
form.fields['user_name'].initial = username
form.fields['building_id'].initial = user_object
if form.is_valid():
announcement = form.save()
messages.success(request, 'Dodano ogłoszenie')
return redirect("map_neigh:index")
else:
for msg in form.error_messages:
messages.error(request, str(msg) + ": " + str(form.error_messages[msg]))

return render(request = request,
template_name = "map_neigh/add.html",
context={"form":form})

form = NewAnnouncementForm
return render(request = request,
template_name = "map_neigh/add.html",
context={"form":form})

几个小时以来我面临的问题是,在尝试提交表单后,我收到错误:'NewAnnouncementForm'没有属性'error_messages'相同的错误消息脚本适用于注册及其表单所以我不认为它与进口有关。

最佳答案

您不应使用.error_messages。这是一个字典,用于显示某个字段可能的错误消息。表单有 .errors attribute [Django-doc]指定错误:

for field, items in form<b>.errors.items()</b>:
for item in items:
messages.error(request, '{}: {}'.format(field, item))

关于python - FormName 没有属性 'error_messages',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60102412/

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