2. Project
Now we are going to create a Django project using Docker.
First, create a requirements.txt file at the root of your project with the following content:
Django
pillow
We are defining the dependencies of our project. In this case, Django (the web framework) and Pillow (a library for working with images).
Now create a Django project:
docker compose run --rm django django-admin startproject my_app .
A Django project can contain many applications. Each application is a set of features that can be reused in other projects. For example, a blog, a forum, a comment system, etc. If you have no applications, you have nothing to show on your website. That is why we are going to create an application.
docker compose run --rm django python manage.py startapp my_blog
Add my_blog to the list of installed applications in my_app/settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'my_blog', # New
]
Now you have a Django project called my_app and an application called my_blog.
Let's go into the application folder:
cd my_blog
Basic settings
Edit the my_app/settings.py file to make sure that the database settings, installed applications, and middleware are correct. In principle you do not need to change anything, but it is good to know where these settings are.
Database
A database is an information storage system. Think of it as a spreadsheet, or a giant table, where you can store and retrieve information in an orderly way. For example, you can store each article of your blog in a row. It will not only have content, but also other important data such as the title, the creation date, the tags, etc. Each row is a record in the database with columns. To retrieve the last article you added, you can run a query against the database asking it to give you the last record, or the one with the most recent date, or the one with the longest title, etc.
Django uses SQLite by default, a lightweight and easy-to-use database engine. For this tutorial, SQLite is enough. If you need to use another database engine, you can change the configuration in my_app/settings.py. Django supports many database engines, such as PostgreSQL, MySQL, Oracle, etc. There is no need to change anything.
Starting the server
It is time to start the Django development server.
First we must edit the compose.yml file so that Django keeps running:
services:
django:
build: .
volumes:
- .:/usr/src/app/
ports:
- "8000:8000"
command: python manage.py runserver 0.0.0.0:8000
Now start the server:
docker compose up
Open your browser and go to http://localhost:8000/. You should see a Django welcome page.
Congratulations! Django is now running on your computer.
Configuring the application
Before continuing I need you to run the following command in your project folder only if you use Linux or MacOS.
sudo chown $USER:$USER -R .
This way you will be able to edit the files without any problems.
I won't distract you any longer! Let's continue.
Hello world
Edit the my_blog/views.py file and add the following code:
from django.http import HttpResponse
def hello_world(request):
return HttpResponse('Hello, world!')
Now create a urls.py file in the my_blog folder with the following content:
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello_world, name='hello_world'),
]
And add the new application to the project URLs in my_app/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('my_blog.urls')),
path('admin/', admin.site.urls),
]
Now go to http://localhost:8000/ and you should see the message Hello, world!.
Congratulations, you have just created your first Django application. Perhaps a bit minimalist, but it is a good start.
Let's do something more interesting: add information to the database and display it on the page.
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
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 bookWill you buy me a coffee?
This is how I keep writing without ads or paywalls.
Sure, it's on me!
Comments
There are no comments yet.