简体   繁体   中英

socket.gaierror: [Errno 11001] getaddrinfo failed in django

I am trying to build an ecommerce store with python and django. I am currently building a pay_on_delivery option, and for that, I am sending an email to myself from the email of the user. Here is my OrderForm :

class OrderForm(forms.Form):
    name = forms.CharField(max_length=30)
    email = forms.EmailField()
    city = forms.CharField(max_length=100)
    address = forms.CharField(widget=forms.Textarea())
    zip_code = forms.CharField(min_length=6,max_length=6)
    product = forms.CharField(max_length=200,widget=forms.TextInput(attrs={'placeholder':'Copy and paste the exact same name of the product you want to purchase'}))
    phone = forms.CharField(min_length=10,max_length=10)

    def save(self):
        cleaned_data = self.cleaned_data
        send_mail(
            "Product {cleaned_data['product']} ordered by {cleaned_data['name']}",
            "A product from our bakery has been ordered. Address: {cleaned_data['address']}. Zip code: {cleaned_data['zip']}. Phone: {cleaned_data['phone']}",
            cleaned_data["email"],
            ["rjain1807@gmail.com"],
            fail_silently=False,
        )

And my view function that handles the form submission:

def buy(request,id):
    product = Product.objects.get(id=id)
    form = OrderForm()

    if request.method == 'POST':
        form = OrderForm(data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponse("Your order has been successfully placed!!")
    context = {
        'form':form,
        'product':product,
    }
    return render(request, 'buy_page.html',context)

Now, when I try to place an order, I get this traceback error upon the form submission:

Internal Server Error: /products/1/buy/
Traceback (most recent call last):
  File "C:\Users\Dell\Desktop\Django\apnibakery\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Dell\Desktop\Django\apnibakery\venv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Dell\Desktop\Django\apnibakery\bakery\apnibakeryy\views.py", line 33, in buy
    form.save()
  File "C:\Users\Dell\Desktop\Django\apnibakery\bakery\apnibakeryy\forms.py", line 20, in save
    fail_silently=False,
  File "C:\Users\Dell\Desktop\Django\apnibakery\venv\lib\site-packages\django\core\mail\__init__.py", line 61, in send_mail
    return mail.send()
  File "C:\Users\Dell\Desktop\Django\apnibakery\venv\lib\site-packages\django\core\mail\message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "C:\Users\Dell\Desktop\Django\apnibakery\venv\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "C:\Users\Dell\Desktop\Django\apnibakery\venv\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python37\lib\smtplib.py", line 307, in _get_socket
    self.source_address)
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python37\lib\socket.py", line 707, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\Dell\AppData\Local\Programs\Python\Python37\lib\socket.py", line 752, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

What is wrong here?

It seems like you are not able to resolve the hostname. What EMAIL_HOST and EMAIL_PORT do you have set in your Django settings? Make sure you can resolve the hostname properly, by entering those infos in this simple script:

import socket
socket.getaddrinfo('yourhostname.com', 80)

I was too confronting the same error for a whole day even though i tried to fix it out then, fortunately, I got to know that, It might have occurred due to one of these errors.

  1. Server error
  2. Unstable internet connection.

In my case, i encountered case 2 then fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM