【发布时间】:2017-06-06 14:47:17
【问题描述】:
我希望有一个视图,可以在我创建的模型“联系人”中添加新联系人。 下面是一些关注的代码行
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
【问题讨论】:
-
你忘记了导入
-
不同的文件有什么关系?哪个文件给出了错误信息?进口是什么样子的?
-
明白了,非常感谢
-
如果问题得到解答,请采纳。