NameError: name '' is not defined

NameError: name '' is not defined

我想要一个视图,可以在我创建的模型 "contacts" 中添加新联系人。 下面是一些关注的代码行

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
})

forms.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

在顶部添加视图,

from .forms import NouveauContactForm

您忘记导入表格了。