Python “如何解决Django错误”;gaierror:[Errno-2]名称或服务未知";?
我正在创建一个演示电子商务网站,在教程的帮助下学习Django,但不幸的是,它只停留在一个地方。我现在可以发送电子邮件表单来提交表单 它抛出了如下错误:Python “如何解决Django错误”;gaierror:[Errno-2]名称或服务未知";?,python,django,Python,Django,我正在创建一个演示电子商务网站,在教程的帮助下学习Django,但不幸的是,它只停留在一个地方。我现在可以发送电子邮件表单来提交表单 它抛出了如下错误: Internal Server Error: /contact/ Traceback (most recent call last): File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/handlers/exception.py
Internal Server Error: /contact/ Traceback (most recent call last): File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request) File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request) File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/jai/Desktop/tryten/src/contact/views.py", line 18, in contact
send_mail(subject, message,emailFrom, emailTo, fail_silently=True) File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/mail/__init__.py", line 62, in send_mail
return mail.send() File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/mail/message.py", line 342, in send
return self.get_connection(fail_silently).send_messages([self]) File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 100, in send_messages
new_conn_created = self.open() File "/home/jai/Desktop/tryten/local/lib/python2.7/site-packages/django/core/mail/backends/smtp.py", line 58, in open
self.connection = connection_class(self.host, self.port, **connection_params) File "/usr/lib/python2.7/smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port) File "/usr/lib/python2.7/smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout) File "/usr/lib/python2.7/smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout) File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM): gaierror: [Errno -2] Name or service not known
这是一段代码,显示发送电子邮件的设置文件中的更改
setting.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'jaizebra@gmail.com'
EMAIL_HOST_PASSWORD = '*************'
EMAIL_PORT = '587'
EMAIL_USE_TLS = True
这就是我正在使用的视图
View.py
from django.shortcuts import render
from .forms import contactForm
from django.core.mail import send_mail
from django.conf import settings
# Create your views here.
def contact(request):
form = contactForm(request.POST or None)
if form.is_valid():
name = form.cleaned_data['name']
comment = form.cleaned_data['comment']
subject = 'Message from MYSITE.com'
message = '%s %s' % (comment, name)
emailFrom = form.cleaned_data['email']
emailTo = [settings.EMAIL_HOST_USER]
send_mail(subject, message,emailFrom, emailTo, fail_silently=True)
context = locals()
template = 'contact.html'
return render(request, template, context)
表格如下:
forms.py
from django import forms
class contactForm(forms.Form):
name = forms.CharField(required=False, max_length=100, help_text='100 Characters max.')
email = forms.EmailField(required=True)
comment = forms.CharField(required=True, widget=forms.Textarea)
更新:
我苦苦挣扎,发现这个错误是因为我在代理背后工作。当我在没有代理的网络上运行这段代码时,它运行得很好
我应该在何处更改设置以添加代理
我在英国也试过类似的东西
将下面的行添加到“wsgi.py”中:
导入操作系统
....
os.environ['http_proxy']=”http://myproxy:port"
os.environ['https_proxy']=”http://myproxy:port"
但它不起作用:在我看来,您正在本地运行此功能。您是否在本地运行SMTP服务器?当您运行
telnet 127.0.0.1 25时,您能连接吗?
?如果您不想进入处理配置,Mailchimp提供了很好的电子邮件服务。@OluwafemiSule显示错误“Trying 127.0.0.1…telnet:无法连接到远程主机:连接被拒绝”您检查了吗?哦,您没有设置电子邮件服务器。如果您使用UbuntuLinux,如果您想在本地运行电子邮件服务器,这里有一个社区提供的关于设置Postfix的指南。