sites_query = connection.execute(“选择域FROM django_site”)psycopg2有没有属性执行

问题描述 投票:0回答:1

[当我尝试启动Gunicorn时,出现此错误:

文件“ /home/django-project/projectfolder/settings.py”,第270行,在ALLOWED_HOSTS = get_allowed_hosts(DATABASES ['default'])get_allowed_hosts中的第16行的文件“ /home/django-project/projectfolder/allowed_hosts.py”sites_query = connection.execute(“从django_site选择域”)AttributeError:“ psycopg2.extensions.connection”对象没有属性“ execute

from .settings import *

DEBUG = False


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'projectname_settings',
        'USER': '******',
        'PASSWORD': '******',
        'HOST': 'localhost',
        'PORT': '',
    }
}

ALLOWED_HOSTS = [
    "mydomain.com",
] + get_allowed_hosts(DATABASES['default'])

Allowed_hosts.py

def get_allowed_hosts(db_params):
    connection = None

    if db_params['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
        import psycopg2
        connection = psycopg2.connect(user=db_params['USER'],
                                      password=db_params['PASSWORD'],
                                      host=db_params['HOST'],
                                      port=db_params['PORT'],
                                      database=db_params['NAME'])
    elif db_params['ENGINE'] == 'django.db.backends.sqlite3':
        import sqlite3
        connection = sqlite3.connect(db_params['NAME'])

    if connection is not None:
        sites_query = connection.execute("SELECT domain FROM django_site")
        sites_result = sites_query.fetchall()
        sites = ["." + site[0] for site in sites_result]
        print("Allowed hosts")
        print(sites)
        return sites
django postgresql gunicorn psycopg2 django-settings
1个回答
0
投票

您已使用cursor对象执行查询:

cursor = connection.cursor()
sites_query = cursor.execute("SELECT domain FROM django_site")
sites_result = cursor.fetchall()

获取数据后不要忘记关闭连接:

cursor.close()
connection.close()
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.