我已经创建如下形式:如何增加Django的forms.Form CharField面积
class ContactForm(forms.Form):
full_name = forms.CharField(required=False)
email = forms.EmailField()
message = forms.CharField(widget=forms.Textarea(attrs={'class':'special', 'size': '40'}))
但是,当我在信息框中添加数据有一些数据是不是我用这种形式相关的电子邮件来了使用Settings.py中提到的设置。但是,full_name和电子邮件会很好。
我的看法是:
def contact(request):
title = "Contact Us"
form = ContactForm(request.POST or None)
if form.is_valid():
form_email = form.cleaned_data.get('email')
form_message = form.cleaned_data.get('message')
form_full_name = form.cleaned_data.get('full_name')
subject = "Site Contact Form"
from_email = settings.EMAIL_HOST_USER
to_email = [from_email, [email protected]']
contact_message = "%s: %s via %s"%(form_full_name, form_message, form_email)
html_template = "<h1>Hello There</h1>"
send_mail(subject, contact_message, from_email, to_email, html_message=html_template, fail_silently=True)
context = {
"form":form,
"title": title
}
return render(request, 'contact/form.html', context)
此外,我需要知道什么是创建某种形式的直接收到信息到我的邮箱是最好的选择。我应该使用基于模型的表单还是没有模型的简单表单?请指教,因为我的消息字段文本不会出现在电子邮件中,但名称和电子邮件会很好。
http://stackoverflow.com/questions/9323886/charfield-increase-width –
你意识到你在你的视图代码右侧有一个语法错误? “to_email”任务中缺少''''。 – Wtower
对于Usman Maqbool你有没有注意到我正在尝试在FormForm中增加size.Form的大小。在ModelsForm中,我可以简单地使用TextField,文本框将会变得更宽。 –