我正在创建一个演示电子商务网站,以便在教程的帮助下学习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", 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)
请帮我解决问题..谢谢。
更新
我经常挣扎并发现,这个错误正在发生,因为我在代理服务器后面工作。何时,我使用没有代理的网络运行此代码,它运行正常。
我应该在设置中添加代理进行更改。
我在
中尝试过类似的东西import os
....
os.environ['http_proxy'] = "http://myproxy:port"
os.environ['https_proxy'] = "http://myproxy:port"
但它不起作用: