gpt4 book ai didi

python - NameError:名称 '' 未定义

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

我想要一个 View ,可以在其中在我创建的模型“联系人”中添加新联系人。下面是一些关心的代码行

views.py:

<小时/>
def contact(request):
form = ContactForm(request.POST or None)
if form.is_valid():
sujet = form.cleaned_data['sujet']
message = form.cleaned_data['message']
envoyeur = form.cleaned_data['envoyeur']
renvoi = form.cleaned_data['renvoi']
envoi = True
return render(request, 'blog/contact.html', locals())

def nouveau_contact(request):
sauvegarde = False
form = NouveauContactForm(request.POST or None, request.FILES)
if form.is_valid():
contact = Contact()
contact.nom = form.cleaned_data["nom"]
contact.adresse = form.cleaned_data["adresse"]
contact.photo = form.cleaned_data["photo"]
contact.save()
sauvegarde = True
return render(request, 'blog/newcontact.html', {
'form': form,
'sauvegarde': sauvegarde
})
<小时/>

表单.py:

<小时/>
class ContactForm(forms.Form):
sujet = forms.CharField(max_length=100)
message = forms.CharField(widget=forms.Textarea)
envoyeur = forms.EmailField(label="Votre adresse mail")


class NouveauContactForm(forms.Form):
nom = forms.CharField()
adresse = forms.CharField(widget=forms.Textarea)
photo = forms.ImageField()

class Contact(models.Model):
nom = models.CharField(max_length=255)
adresse = models.TextField()
photo = models.ImageField(upload_to="photos/")

def __str__(self):
return self.nom

错误消息告诉我“NewContactForm”未定义 this is the ERROR_MESSAGE

最佳答案

添加顶部的 View ,

from .forms import NouveauContactForm

您忘记导入表单。

关于python - NameError:名称 '' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44393191/

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