Guía 23: Django - Despliegue Python Anywhere#
Actividades previas#
Firebase Admin Python SDK: Clave privada#
En Firebase Console, acceda a su proyecto landing.
Acceda a Configuración de proyecto > Cuentas de servicio > SDK de Firebase Admin para generar la clave privada.
PythonAnywhere#
Obtenga una cuenta Beginner account en PythonAnywhere.
Utilice su cliente de IAG generativa para explicar la utilidad de PythonAnywhere.
Actividades en clases#
PythonAnywhere#
Consola (Console)#
En PythonAnywhere, acceda a la opción Consoles.
Cree una nueva consola en Start a new console: > Other: Bash.
Desde la línea de comandos:
Cree un entorno virtual con el nombre environment y con la versión de Python 3.10
mkvirtualenv --python=/usr/bin/python3.10 env
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
Instale las librerías de requirements.txt, con:
pip install -r requirements.txt
Archivos (Files)#
Clave privada de Firebase Admin SDK#
En PythonAnywhere, acceda a la opción Files.
Acceda a la carpeta django_api_suite.
Cree la carpeta secrets y suba el archivo de clave privada de Firebase Admin SDK, con el nombre
landing-key.json.
Seguridad#
Edite el archivo
django_api_suite/backend_data_server/settings.pyel dominio ALLOWED_HOSTS agregue el dominio de su WebApp... ALLOWED_HOSTS = ['<USUARIO-PYTHONANYWHERE>.pythonanywhere.com']
Archivos estáticos#
Edite el archivo
django_api_suite/backend_data_server/settings.pyy modifique la configuración de archivos estáticos:... STATICFILES_DIRS = [ ... ] STATIC_ROOT = "assets/"
Guarde los cambios en el archivo
django_api_suite/backend_data_server/settings.py.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
assetsen la raíz del proyecto.
Aplicación Web (WeApp)#
En PythonAnywhere, acceda a la opción WebApp.
Seleccione la opción » Manual configuration (including virtualenvs), con la versión de Python 3.10
En la interfaz de la WebApp:
En la sección Virtualenv, establezca la ruta al entorno virtual
/home/<USUARIO-PYTHONANYWHERE>/.virtualenvs/env/.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/.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.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.
Guarde los cambios en el archivo
wsgi.py.
En la sección Web, haga clic en el botón Reload para reiniciar la WebApp y aplicar los cambios.
Revise los cambios en el navegador en la URL: http://<USUARIO-PYTHONANYWHERE>.pythonanywhere.com/.
Conclusiones#
Actividades autónomas#
Recursos extras#
En redes:
Top 5 Reasons Why PythonAnywhere Should Be Your Next Project's Home
— DavidayoTech (@DavidayoAI) December 27, 2024
1. Zero Setup Hassle
2. Collaboration Made Easy
3. Always Available, Anywhere Access
4. Scales With Your Needs
5. Fantastic for Web Apps
Over to you: What are your go-to tools for Python development? #python pic.twitter.com/je9mAEH0jf