Django联络表格

时间:2019-02-03 17:21:28

标签: python django forms contacts

我使用django创建了一个联系表单,用于发送姓名,电子邮件和消息。表单成功通过了名称,主题附加,但是只有部分邮件到达了gmail,而不是整个邮件。我如何获取完整的消息以在gmail帐户上向您显示?

from django.db import models

# Create your models here.
class Contact(models.Model):

    name = models.CharField(max_length=30)
    email = models.EmailField()
    message = models.TextField()


    def __str__(self):
        return self.name

表格

from django import forms
from django.forms import ModelForm
from .models import Contact

class ContactForm(forms.ModelForm):

    class Meta:
        model = Contact
        fields = ('name', 'email', 'message',)

观看次数

def contact(request):
mapbox_access_token = 'pk.my_mapbox_access_token'
if request.method == 'GET':
    form = ContactForm()
else:
    form = ContactForm(request.POST)
    if form.is_valid():
        sender_name = form.cleaned_data['name']
        emailFrom = form.cleaned_data['email']
        message = "{0} has sent you a new message:\n\n{1}".format(sender_name, form.cleaned_data['message'])


        form.save()
        try:
            send_mail('New Enquiry', message, emailFrom, ['va.glazing@gmail.com'],fail_silently=False, )
        except BadHeaderError:
            return HttpResponse('Invalid header found')
        return redirect('success')

return render(request, "contact.html",{'form': form})
#settings

EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '****.***.com'
EMAIL_HOST_PASSWORD = '******'
EMAIL_PORT = 587
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

这是电子邮件通过后得到的结果的图像。

https://i.stack.imgur.com/31SBv.png

https://i.stack.imgur.com/5MGgc.png

2 个答案:

答案 0 :(得分:0)

尝试将'subject': forms.TextInput(attrs={'': ''})添加到widgets字典中,类似于表格。他们似乎正在碰撞。

答案 1 :(得分:0)

来自Django Docs

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)

如您所见,您在错误的位置编写了邮件和电子邮件。切换位置应该可以解决问题。

name将是您的主题,email将是正文,message是消息内容。