2. Create project

We create the virtual environment.

python3 -m venv venv
source venv/bin/activate

Now we create a plain file, with a txt extension, called requirements.txt. It will be the place where we list the dependencies to install.

django
djangorestframework
# Testing tool
pytest
# Integration with Django
pytest-django
# Random data generator for the tests
Faker

We install the dependencies.

pip3 install -r requirements.txt

Now we create our project, with the name proyecto.

django-admin startproject proyecto .

Inside it we can have many applications. For now we will create libros.

mkdir -p app/libros
python3 manage.py startapp libros app/libros

Edit the application's path.

# app/libros/apps.py
from django.apps import AppConfig

class LibrosConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'app.libros' # Before: name = 'libros'

Add libros to the INSTALLED_APPS configuration in the settings.py file:

# proyecto/settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework', # new
    'app.libros', # new
]

We will apply the migrations for the database.

python3 manage.py makemigrations
python3 manage.py migrate

Since we have not indicated otherwise, it will use the fantastic SQLite database. You can learn a lot more about it in my SQL Course. After this aside, let's continue.

We create a super user to manage the admin panel.

python3 manage.py createsuperuser

We start the server.

python3 manage.py runserver

We open the following address in a browser: http://localhost:8000/. It will show us a welcome screen telling us that everything went fine and Django is ready to work on.

If you want to access the admin panel with the super user created earlier, you just need to go to http://localhost:8000/admin/.

This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.

Desafíos de programación atemporales y multiparadigmáticos

Desafíos de programación atemporales y multiparadigmáticos

Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.

Buy the book

Will you buy me a coffee?

This is how I keep writing without ads or paywalls.

Comments

There are no comments yet.