How to resolve Django error "gaierror: [Errno -2] Name or service not known"?

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 = '[email protected]'
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'] = ""
os.environ['https_proxy'] = ""

but it's not working:

9 Related questions 1 ImproperlyConfigured: You need to specify NAME in your Django settings file 1 Can't identify Django error 0 Name Error Django Related questions 1 ImproperlyConfigured: You need to specify NAME in your Django settings file 1 Can't identify Django error 0 Name Error Django 0 Django - OperationalError: could not connect to server 2 Django 'global name' error 0 Getting TypeError when I am trying to run server using Django? 0 Why when I run Django server it fails? 0 Django: ImportError: No module named 'services' 1 django sending mail [Errno -2] Name or service not known 0 Getting error when run django server Load 7 more related questions Show fewer related questions Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like