如何增加Django表单.Form CharField Size

时间:2016-02-17 09:43:23

标签: django forms email

我创建了以下表单:

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, myemail@gmail.com']
        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)

此外,我需要知道创建一些表单以直接向我的电子邮件收到信息的最佳选择。我应该使用基于模型还是没有模型的简单形式?请告知我的消息字段文本没有出现在电子邮件中,但名称和电子邮件正常。

1 个答案:

答案 0 :(得分:1)

试试这个,

message = forms.CharField(widget=forms.TextInput(attrs={'class':'special', 'size': '40'}))