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.
Building SPAs with Django and HTML Over the Wire: Learn to build real-time single page applications with Python
The HTML over WebSockets approach simplifies single-page application (SPA) development and lets you bypass learning a JavaScript rendering framework such as React, Vue, or Angular, moving the logic to Python. This web application development book provides you with all the Django tools you need to simplify your developments with real-time results.
Buy the bookHelp me keep writing
Every coffee gives me a push toward the next article.
Sure, it's on me!
Comments
There are no comments yet.