2016-03-07 92 views
2

我想發送電子郵件是使用django.core.mail.send_mail()。但是,當我嘗試提交表單時出現以下錯誤。在Django中設置代理

這裏是我得到的錯誤:

Request Method: POST 
Request URL: http://127.0.0.1:8000/contact/ 

Django Version: 1.8.10 
Python Version: 2.7.6 
Installed Applications: 
('django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'newsletter', 
'Contact') 
Installed Middleware: 
('django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware', 
'django.middleware.security.SecurityMiddleware') 


Traceback: 
File "/home/sumit/Desktop/Home/Django/local/lib/python2.7/site-packages/Django-1.8.10-py2.7.egg/django/core/handlers/base.py" in get_response 
    132.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/home/sumit/Desktop/Home/Django/src/Contact/views.py" in contact 
    25.   fail_silently=False 
File "/home/sumit/Desktop/Home/Django/local/lib/python2.7/site-packages/Django-1.8.10-py2.7.egg/django/core/mail/__init__.py" in send_mail 
    62.  return mail.send() 
File "/home/sumit/Desktop/Home/Django/local/lib/python2.7/site-packages/Django-1.8.10-py2.7.egg/django/core/mail/message.py" in send 
    303.   return self.get_connection(fail_silently).send_messages([self]) 
File "/home/sumit/Desktop/Home/Django/local/lib/python2.7/site-packages/Django-1.8.10-py2.7.egg/django/core/mail/backends/smtp.py" in send_messages 
    100.    new_conn_created = self.open() 
File "/home/sumit/Desktop/Home/Django/local/lib/python2.7/site-packages/Django-1.8.10-py2.7.egg/django/core/mail/backends/smtp.py" in open 
    58.    self.connection = connection_class(self.host, self.port, **connection_params) 
File "/usr/lib/python2.7/smtplib.py" in __init__ 
    256.    (code, msg) = self.connect(host, port) 
File "/usr/lib/python2.7/smtplib.py" in connect 
    316.   self.sock = self._get_socket(host, port, self.timeout) 
File "/usr/lib/python2.7/smtplib.py" in _get_socket 
    291.   return socket.create_connection((host, port), timeout) 
File "/usr/lib/python2.7/socket.py" in create_connection 
    553.  for res in getaddrinfo(host, port, 0, SOCK_STREAM): 

Exception Type: gaierror at /contact/ 
Exception Value: [Errno -2] Name or service not known 

這是我views.py文件。

from django.shortcuts import render 
from .forms import ContactUsForm 
from django.core.mail import send_mail 
from django.conf import settings 

def contact(request): 
    form = ContactUsForm(request.POST or None) 
    context = { 
     "title" : "Contact", 
     "form" : form, 
    } 
    if form.is_valid(): 
     form_email = form.cleaned_data.get("email") 
     form_full_name = form.cleaned_data.get("full_name") 

     subject = "Sent via django" 
     from_email = settings.EMAIL_HOST_USER 
     to_email = [form_email] 
     contact_message = "Thank you %s " %(form_full_name) 

     send_mail(subject, 
      contact_message, 
      from_email, 
      to_email, 
      fail_silently=False 
      ) 
     context = { 
      "title" : "Thank you for your feedback ! " 
     } 
     form.save() 
    return render(request, "contact.html", context) 

forms.py:

from django import forms 
from .models import ContactUs 

class ContactUsForm(forms.ModelForm): 
    class Meta: 
     model = ContactUs 
     fields = ["full_name", "email", "message"] 

models.py

from django.db import models 

# Create your models here. 
class ContactUs(models.Model): 
    full_name = models.CharField(max_length=120, blank=True) 
    email = models.EmailField() 
    message = models.CharField(max_length=100, blank=True) 
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False) 
    updated = models.DateTimeField(auto_now=True, auto_now_add=False) 

    def __unicode__(self): 
     return self.email 

我在終端使用設置代理變量:

export https_proxy="https://username:[email protected]:myport" 
export http_proxy="http://username:[email protected]:myport" 
export socks_proxy="socks://username:[email protected]:myport" 
export ftp_proxy="ftp://username:[email protected]:myport" 

我該如何解決這個錯誤?請幫忙。

回答

1

您是否在settings.py文件中設置了必需的參數?

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

而且其gaierror在您的主機的參數應該是:

HOST = ''(不空間)

不要用空間使用HOST = ' '