簡體   English   中英

socket.gaierror: [Errno 11001] getaddrinfo 在 django 中失敗

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

我正在嘗試使用 python 和 django 建立一個電子商務商店。 我目前正在構建一個pay_on_delivery選項,為此,我從用戶的 email 向自己發送 email。 這是我的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,
        )

我認為處理表單提交的 function :

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)

現在,當我嘗試下訂單時,我在提交表單時收到此回溯錯誤:

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

這里有什么問題?

您似乎無法解析主機名。 您在 Django 設置中設置了哪些EMAIL_HOST和 EMAIL_PORT? 通過在這個簡單的腳本中輸入這些信息,確保您可以正確解析主機名:

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

即使我當時試圖修復它,我也面對同樣的錯誤一整天,幸運的是,我知道,它可能是由於其中一個錯誤而發生的。

  1. 服務器錯誤
  2. 不穩定的互聯網連接。

就我而言,我遇到了案例 2,然后修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM