Vamos a desarrollar la aplicación del tutorial de django 3.1. Vamos a configurar tu equipo como entorno de desarrollo para trabajar con la aplicación.


  • Realizamos un fork de la aplicación añadiendola a nuestros repositorios y seguidamente lo clonaremos en nuestra maquina:
alejandrogv@AlejandroGV:~/Escritorio/ASIR/IWEB/despliegue_python$ git clone git@github.com:alepeteporico/django_tutorial.git
  • Crearemos el entorno virtual donde instalaremos las dependencias necesarias para hacer funcionar nuestra aplicación.
alejandrogv@AlejandroGV:~/Escritorio/ASIR/IWEB$ python3 -m venv django

(django) alejandrogv@AlejandroGV:~/Escritorio/ASIR/IWEB$ pip install -r despliegue_python/django_tutorial/requirements.txt
  • En el fichero settings.py podremos comprobar con que base de datos vamos a trabajar. Efectivamente, es una base de datos sqlite con el nombre db.sqlite3
(django) alejandrogv@AlejandroGV:~/Escritorio/ASIR/IWEB/despliegue_python/django_tutorial$ cat django_tutorial/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}
  • Crearemos la base de datos.
(django) alejandrogv@AlejandroGV:~/Escritorio/ASIR/IWEB/despliegue_python/django_tutorial$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying polls.0001_initial... OK
  Applying sessions.0001_initial... OK
  • Y seguidamente tendremos que crear un usuario para administrar dicha base de datos.
(django) alejandrogv@AlejandroGV:~/Escritorio/ASIR/IWEB/despliegue_python/django_tutorial$ python3 manage.py createsuperuser
Username (leave blank to use 'alejandrogv'): admin
Email address: tojandro@gmail.com
Password: 
Password (again): 
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
  • Ahora ejecutaremos el servidor y entraremos en la zona de admin para comprobar su funcionamiento.

(django) alejandrogv@AlejandroGV:~/Escritorio/ASIR/IWEB/despliegue_python/django_tutorial$ python3 manage.py runserver

admin

admin

  • Crearemos dos preguntas con posibles respuestas.

pregunta1

pregunta2

preguntas

  • Comprobaremos que funciona la url /polls

polls

Entorno de producción

  • Clonamos nuevamente el repositorio, esta vez en nuestra VPS.
debian@mrrobot:~/aplicaciones$ git clone https://github.com/alepeteporico/django_tutorial.git
  • Como hicimos anteriormente, crearemos un entorno virtual donde instalaremos las dependencias necesarias y como novedad debemos instalar el paquete que permitirá a python trabajar con mysql.
pip install mysqlclient
  • Nos dirigimos a mariadb donde crearemos una base de datos y un usuario con privilegios sobre ella.
MariaDB [(none)]> create database django_tutorial;

MariaDB [(none)]> grant all on django_tutorial.* to 'admin'@'%' identified by 'admin' with grant option;
  • Ahora configuramos el fichero settings,py de la aplicación para añadir esta base de datos.
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_tutorial',
        'USER': 'admin',
        'PASSWORD': 'admin',
        'HOST': 'db.alejandrogv.site',
        'PORT': '3306',
    }
}
  • Realizamos la migración.
(django) debian@mrrobot:~/aplicaciones/django_tutorial$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying polls.0001_initial... OK
  Applying sessions.0001_initial... OK
  • Y creamos un super usuario.
(django) debian@mrrobot:~/aplicaciones/django_tutorial$ python3 manage.py createsuperuser
Username (leave blank to use 'debian'): admin
Email address: tojandro@gmail.com
Password: 
Password (again):
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
  • Usaremos gunicorn como servidor de aplicaciones python, para ello debemos instalarlo.
pip install gunicorn
  • Vamos a crear una unidad de systemd para poder tener encendida nuestra aplicación continuamente.
[Unit]
Description=django_tutorial
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
User=www-data
Group=www-data
Restart=always

ExecStart=/home/debian/entornos/django/bin/gunicorn django_tutorial.wsgi
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

WorkingDirectory=/home/debian/aplicaciones/django_tutorial
Environment=PYTHONPATH='/home/debian/aplicaciones/django_tutorial:/home/debian/entornos/django/lib/python3.9/site-packages'

PrivateTmp=true
  • Creamos el virtual host en nginx.
server {
        listen 80;
        listen [::]:80;

        root /home/debian/aplicaciones/django_tutorial;

        index index.html index.php index.htm index.nginx-debian.html;

        server_name django.alejandrogv.site;

        location / {
                proxy_pass http://localhost:8000;
                include proxy_params;
        }

        location /static {
                alias /home/debian/entornos/django/lib/python3.9/site-packages/django/contrib/admin/static;
        }
}
  • Creamos el enlace a sites-enabled.
sudo ln -s /etc/nginx/sites-available/django_tutorial.conf /etc/nginx/sites-enabled/
  • Debemos añadir el host de acceso a la línea de allowed host en el fichero settings.py de la aplicación.
ALLOWED_HOSTS = ['django.alejandrogv.site']
  • Comprobamos que podemos acceder.

acceso

  • Ahora para que no aparezcan errores de ejecución mientras se está sirviendo la aplicación cambiamos la línea de debug a false en el fichero settings.py y reiniciamos nginx y la unidad de systemdb que creamos.
DEBUG = False

Modificación de la aplicación.

  • Primero realizaremos estos cambios en el entorno de desarrollo, el primero de ellos será que nuestro nombre aparezca en la pagina de admin. Para ello modificaremos el fichero django_tutorial/polls/templates/polls/index.html.
{% load static %}

<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
Alejandro Gutierrez Valencia
{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

nombre

  • En django_tutorial/polls/templates/index.html cambiaremos la imagen que aparece de fondo en la aplicación.

imagen

  • Añadiremos una nueva tabla en la base de datos, para ello añadimos el siguiente modelo a polls/models.py
class Categoria(models.Model):
        Abr = models.CharField(max_length=4)
        Nombre = models.CharField(max_length=50)

        def __str__(self):
                return self.Abr+" - "+self.Nombre
  • El siguiente paso es crear una nueva migración:
(django) alejandrogv@AlejandroGV:~/django_tutorial$ python3 manage.py makemigrations
Migrations for 'polls':
  polls/migrations/0002_categoria.py
    - Create model Categoria
  • Migramos.
(django) alejandrogv@AlejandroGV:~/django_tutorial$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, polls, sessions
Running migrations:
  Applying polls.0002_categoria... OK
  • Ahora en polls/admin.py debemos añadir el nuevo modelo.
from .models import Choice, Question, Categoria

admin.site.register(Categoria)

categoria

  • Después de hacer estos cambios, la forma de migrarlos en subir los cambios a github y hacer un pull en el entorno de desarrollo.
debian@mrrobot:~/aplicaciones/django_tutorial$ git pull
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 9 (delta 6), reused 9 (delta 6), pack-reused 0
Unpacking objects: 100% (9/9), 990 bytes | 247.00 KiB/s, done.
From https://github.com/alepeteporico/django_tutorial
   2e1a38a..6b3b604  master     -> origin/master
Updating 2e1a38a..6b3b604
Fast-forward
 polls/admin.py                   | 3 ++-
 polls/models.py                  | 7 +++++++
 polls/templates/index.html       | 4 ++--
 polls/templates/polls/index.html | 4 ++--
 4 files changed, 13 insertions(+), 5 deletions(-)
  • Comprobamos que se han migrado los cambios.

cambios

cambios

cambios