¿Cómo usar Django para obtener el nombre del servidor host?


¿Cómo usar Django para obtener el nombre del servidor host?

¿Necesito el nombre del servidor de alojamiento en lugar del nombre del cliente?

Author: Ron, 2010-11-04

4 answers

Generalmente pongo algo como esto en settings.py:

import socket

try:
    HOSTNAME = socket.gethostname()
except:
    HOSTNAME = 'localhost'
 65
Author: Craig Trader,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-11-05 04:15:55

Si tiene una solicitud (por ejemplo, esto está dentro de una vista), puede mirar request.get_host() lo que le da un locname completo (host y puerto), teniendo en cuenta los encabezados del proxy inverso si los hay. Si no tiene una solicitud, debe configurar el nombre de host en algún lugar de su configuración. Solo mirar el nombre de host del sistema puede ser ambiguo en muchos casos, los hosts virtuales son los más comunes.

 66
Author: Tobu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-18 22:03:22

Intenta os.environ.get('HOSTNAME')

 3
Author: Ankit Jaiswal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-18 02:23:20

Solo agrega a la respuesta de@Tobu. Si tiene un objeto request y desea conocer el protocolo (es decir, http / https), puede usar request.scheme (como sugiere el comentario de@RyneEverett).

Alternativamente, puede hacer (respuesta original a continuación):

if request.is_secure():
    protocol = 'https'
else:
    protocol = 'http'

Porque is_secure() devuelve True si la solicitud se realizó con HTTPS.

 3
Author: azalea,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-27 20:52:44