Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Creating a Cookie-Cutter Django App

For more information on the tools that you’re using in this lesson, check out:

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.

00:19 To that end, you can take the following steps to set up your app. First, SSH into your VM and make sure that you have the latest versions of Python and SQLite3 installed.

00:48 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 python3.11.

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.

01:57 You may well not need to do this, but the code to do so is included in the course files. You can now bootstrap the Django project and app using Django’s management commands, as seen on-screen.

02:37 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.

03:11 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.

03:46 To save a file in nano, press Control and O for output and hit Enter. You can then exit by pressing Control and X.

03:59 Next, open myapp/templates/myapp/home.html and create a short and sweet HTML page.

04:35 After that, edit myapp/views.py to render that HTML page.

04:59 Now, create and open myapp/urls.py to associate your new view with a URL pattern.

05:24 And now, edit project/urls.py to include the URLs you’ve just added.

05:57 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.

06:23 You can do this by pressing Control + K at the beginning of the line. Then enter the code seen on-screen to read the secret key from the environment and raise an error if it’s not found.

06:44 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.

07:08 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.

07:40 Also, you can print out the value of the environment variable to check that the script has correctly stored the value. With all that in place, you are ready to go.

07:52 Your minimal app is complete and set up. In the next section of the course, you’ll see how to get the site up and running with Django’s development server.

Become a Member to join the conversation.