我的联系表单无法在Django中正常运行吗?

时间:2019-04-11 11:46:06

标签: python django

这是一个随机用户可以填写联系表并可以向管理员发送消息的应用程序。 该代码未将电子邮件发送到我的电子邮件地址。

错误: / shop / contact /中的TimeoutError [WinError 10060]连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接的主机未能响应,建立的连接失败

models.py

class Contact(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField()
    phone = models.CharField(max_length=15)
    subject = models.CharField(max_length=100)
    message = models.TextField()

    def __str__(self):
        return self.subject

views.py

def contact(request):
    if request.method == "POST":
        form = ContactForm(request.POST)
        if form.is_valid():
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']
        email = form.cleaned_data['email']
        #subject = form['subject'].value() tried this also
        #message = form['message'].value()
        #email = form['email'].value()
        recipients = [settings.EMAIL_HOST_USER]
        send_mail(subject, message, email, recipients,fail_silently=False)
            form.save()
            messages.success(request,'Message Sent.')
            return redirect('shop:contact')
        else:
            messages.error(request,'Error in Form')
    else:
        form = ContactForm()
    return render(request,'shop/contact.html',{'form':form})

forms.py

class ContactForm(forms.ModelForm):
    class Meta:
        model = Contact
        fields = ['name','phone','email','subject','message']

contact.html

{% extends "shop/base.html" %}
{% load bootstrap4 %}

<title>{% block title %}Contact{% endblock %}</title>

{% block content %}
 <center>{% bootstrap_messages %}</center>
<div class="container">
    <div class="row">
        <div class="col-lg-4 offset-lg-4 col-sm-10 offset-sm-1">
            <div class="card my-5">

                <div class="card-header text-center">
                   Contact
                </div>
                <div class="card-body">
                    <form action="" method="post">
                        {% csrf_token %}
                        {% bootstrap_form form %}
                        <input type="submit" class="btn btn-success text-center w-100 mb-3" value="Contact">
                    </form>

                </div>
            </div>
        </div>
    </div>
</div>

{% endblock %}

settings.py

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'myemailaddress'
EMAIL_HOST_PASSWORD = 'mypassword'
EMAIL_POST = '587'

0 个答案:

没有答案