Guía 23: Django - Despliegue Python Anywhere#

Actividades previas#

Firebase Admin Python SDK: Clave privada#

  1. En Firebase Console, acceda a su proyecto landing.

  2. Acceda a Configuración de proyecto > Cuentas de servicio > SDK de Firebase Admin para generar la clave privada.

PythonAnywhere#

  1. Obtenga una cuenta Beginner account en PythonAnywhere.

  2. Utilice su cliente de IAG generativa para explicar la utilidad de PythonAnywhere.

Actividades en clases#

PythonAnywhere#

Consola (Console)#

  1. En PythonAnywhere, acceda a la opción Consoles.

  2. Cree una nueva consola en Start a new console: > Other: Bash.

  3. Desde la línea de comandos:

    1. Cree un entorno virtual con el nombre environment y con la versión de Python 3.10

    mkvirtualenv --python=/usr/bin/python3.10 env
    
    1. Clone el repositorio django_api_suite y acceda a la carpeta backend_data_server:

    git clone https://github.com/<USUARIO-GITHUB>/django_api_suite.git
    cd django_api_suite
    
    1. Instale las librerías de requirements.txt, con:

    pip install -r requirements.txt
    

Archivos (Files)#

Clave privada de Firebase Admin SDK#
  1. En PythonAnywhere, acceda a la opción Files.

  2. Acceda a la carpeta django_api_suite.

  3. Cree la carpeta secrets y suba el archivo de clave privada de Firebase Admin SDK, con el nombre landing-key.json.

Seguridad#
  1. Edite el archivo django_api_suite/backend_data_server/settings.py el dominio ALLOWED_HOSTS agregue el dominio de su WebApp

    ...
    ALLOWED_HOSTS = ['<USUARIO-PYTHONANYWHERE>.pythonanywhere.com']
    
Archivos estáticos#
  1. Edite el archivo django_api_suite/backend_data_server/settings.py y modifique la configuración de archivos estáticos:

    ...
    STATICFILES_DIRS = [ ... ]
    
    STATIC_ROOT = "assets/"
    
  2. Guarde los cambios en el archivo django_api_suite/backend_data_server/settings.py.

  3. Desde la interfaz de Python Anywhere, acceda en la opción Console y ejecute el comando:

    python manage.py collectstatic
    

    Nota

    Confirme la creación de la carpeta assets en la raíz del proyecto.

Aplicación Web (WeApp)#

  1. En PythonAnywhere, acceda a la opción WebApp.

  2. Seleccione la opción » Manual configuration (including virtualenvs), con la versión de Python 3.10

  3. En la interfaz de la WebApp:

    1. En la sección Virtualenv, establezca la ruta al entorno virtual /home/<USUARIO-PYTHONANYWHERE>/.virtualenvs/env/.

    2. En el sección Static files, relaciona la URL /static/ con la ruta a la carpeta de archivos estáticos /home/<USUARIO-PYTHONANYWHERE>/django_api_suite/assets/.

    3. En la sección CODE, haga clic en la opción Working directory para modificar la ruta a la carpeta del proyecto /home/<USUARIO-PYTHONANYWHERE>/django_api_suite.

    4. En la sección CODE, haga clic en la opción WSGI configuration file y reemplace el contenido del archivo con el siguiente código:

    # This file contains the WSGI configuration required to serve up your
    # web application at http://<USUARIO-PYTHONANYWHERE>.pythonanywhere.com/
    # It works by setting the variable 'application' to a WSGI handler of some
    # description.
    #
    # The below has been auto-generated for your Django project
    
    import os
    import sys
    
    # add your project directory to the sys.path
    project_home = '/home/<USUARIO-PYTHONANYWHERE>/django_api_suite'
    if project_home not in sys.path:
         sys.path.insert(0, project_home)
    
    # set environment variable to tell django where your settings.py is
    os.environ['DJANGO_SETTINGS_MODULE'] = 'backend_data_server.settings'
    
    # serve django via WSGI
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    

    Nota

    Reemplace <USUARIO-PYTHONANYWHERE> con su nombre de usuario en PythonAnywhere.

    1. Guarde los cambios en el archivo wsgi.py.

  4. En la sección Web, haga clic en el botón Reload para reiniciar la WebApp y aplicar los cambios.

  5. Revise los cambios en el navegador en la URL: http://<USUARIO-PYTHONANYWHERE>.pythonanywhere.com/.

Conclusiones#

Actividades autónomas#

Recursos extras#

En redes: