For more information on the tools that you’re using in this lesson, check out:
Creating a Cookie-Cutter Django App
00:00 Creating a Cookie-Cutter Django App. You’re not concerned with making a fancy Django project with complex URL routing or advanced database features for this course. Instead, you want something that’s plain, small, and understandable, allowing you to test quickly whether your infrastructure is working.
In the case of this VM, Python 3.10 is the system Python and the Python version that ships with Ubuntu 20.04. Upgrading the distribution ensures you receive bug and security fixes from the latest Python release. Optionally, you could install another Python version entirely, such as Python 3.11, alongside the system-wide interpreter, which you’d need to invoke specifically as
01:18 Next, create and activate a virtual environment. Now, install Django 4.1. At this point, for the purposes of on-screen clarity, the terminal prompt will be set to be shorter than the default, as much of each line is being taken up by the prompt.
This creates the Django app
myapp alongside the project named
project. You can see the structure of files created on-screen. As you’re accessing the machine via SSH, you’ll be using a terminal editor such as Vim or GNU nano.
On-screen, you’ll see nano being used, as it’s installed by default in many Linux distributions and is straightforward to learn. Open
project/settings.py and append your app to
INSTALLED_APPS, as seen on-screen.
You can do one more thing while you’re at it, which is to make sure that Django’s secret key used for cryptographic signing isn’t hard-coded in
settings.py, which Git will likely track. Open up
project/settings.py in nano, import the
os module as you will be needing it, and then remove the hard-coded secret key.
This tells Django to look in your environment for
SECRET_KEY rather than including it in your application source code. For larger projects, check out
django-environ to configure your Django application with environment variables. Finally, you’ll need to set the key in your environment.
Here’s how you can do that on Ubuntu Linux using OpenSSL to set the key to an eighty-character string. You can
cat the contents of
.DJANGO_SECRET_KEY to see that
openssl has generated a cryptographically secure hex string key.
Become a Member to join the conversation.