I am creating a demo e-commerce website to learn Django with the help of a tutorial but unfortunately stuck in one place. I am now able to send the email form to submit the form.
Its throwing an error like this:
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
This is the piece of code that shows the changes in the setting file to send the email.
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
This is the view which is I am using.
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)
This is the form:
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)
UPDATE:
I struggle a lot and find that this error was coming because I was working behind the proxy. When I run this code with a network without a proxy, it runs fine.
Where should I make changes in the setting to add proxy?
I have tried something like this in
# lines bellow added to 'wsgi.py' :
import os
....
os.environ['http_proxy'] = "http://myproxy:port"
os.environ['https_proxy'] = "http://myproxy:port"
but it's not working: