【问题标题】:How to increase Django forms.Form CharField Size如何增加 Django forms.Form CharField Size
【发布时间】:2016-02-17 09:43:23
【问题描述】:

我创建了以下表单:

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 和 email 都很好。

我的看法是:

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)

我还需要知道创建一些表单以直接将信息接收到我的电子邮件的最佳选择是什么。我应该使用基于模型的表单还是没有模型的简单表单?请告知,因为我的消息字段文本没有出现在电子邮件中,但姓名和电子邮件很好。

【问题讨论】:

  • 您意识到您的视图代码中有语法错误吧? to_email 分配中缺少 '
  • 致 Usman Maqbool 您是否注意到我正在尝试增加 forms.Form 而不是 ModelForm 中的大小。在 ModelsForm 中,我只需使用 TextField,文本框就会变宽。
  • 您在发送电子邮件之前检查过“contact_message”中的值吗?消息是否存在?
  • 你知道怎么做吗?

标签: django forms email


【解决方案1】:

试试这个,

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

【讨论】:

  • M Hassan 是的,值在那里。
猜你喜欢
  • 2021-07-06
  • 1970-01-01
  • 2017-01-12
  • 2023-01-19
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 2016-01-12
  • 2019-03-21
相关资源
最近更新 更多