NameError:未定义名称''

时间:2017-06-06 14:47:18

标签: python django nameerror

我希望有一个视图,我可以在我创建的模型“联系人”中添加新联系人。 以下是一些与之相关的代码行

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

1 个答案:

答案 0 :(得分:1)

添加顶部的视图,

from .forms import NouveauContactForm

您忘记导入表单。