-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Building SPAs with Django and HTML Over the Wire
By :
We already have a base with which to work with Python; now, it’s time to install the minimum dependencies and tools that will be practical in Django.
We will add the following content to requirements.txt, which is currently empty:
# Django
django===4.0
# Django Server
daphne===3.0.2
asgiref===3.4.1
# Manipulate images
Pillow===8.2.0
# Kit utilities
django-extensions===3.1.3
# PostgreSQL driver
psycopg2===2.9.1
# Django Channels
channels===3.0.4
# Redis Layer
channels_redis===3.2.0
You may not know some of them since they are part of the project that adds WebSocket to Django. Let’s review each one:
PyCharm may suggest you install a plugin, as shown in the following screenshot:
Figure 1.8 – PyCharm asking whether you want to install the new dependencies
If you click on Install plugins, it will show you a window, like so:
Figure 1.9 – PyCharm asking whether you want to install the requirements plugin
By clicking on the OK button, we can enjoy color codes for requirements.txt.
Figure 1.10 – Color codes thanks to the plugin
Now, we will recompile the image so that all the dependencies we have added are installed.
With PyCharm, this can be done in a visual way. Go to Dockerfile, right-click on the double arrow shown in the following screenshot, and select Build Image for ‘Dockerfile’:
Figure 1.11 – Compiling a Dockerfile image using PyCharm
If you are using the terminal or another editor, we will use docker-compose in the directory:
docker-compose build
By recreating the image, we’ve integrated all the dependencies inside the image; now, Django has everything we need. To check that it’s installed and we have version 4, we’ll temporarily modify entrypoint:
Entrypoint: django-admin --version
And then, we’ll run the service.
Remember that you can do this by clicking on the green arrow next to Python (line 5 in Figure 1.12) or through docker-compose.
docker-compose up
Figure 1.12 – Checking which version of Django is installed
In both cases, you can see that it returns 4.0 or the version specified in requirements.txt. We are ready!
All this work can serve as a template for future Python developments. Don’t lose it!
After creating a minimal template through the Django client, we’re going to configure it to launch the test server every time the service is up.