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.

Bootstrap a Django Project

For more information on concepts covered in this lesson, you can check out:

Here are the command-line snippets used in this lesson:

Shell
$ mkdir portfolio-project
$ cd portfolio-project/
$ python3 -m venv venv --prompt portfolio-project
$ tree -L 2
$ ls venv/bin/ | grep activate
$ source venv/bin/activate
$ which python
$ which pip
$ python -m pip install django==3.2.7
$ python -m pip install --upgrade pip
$ pip list
$ pip freeze > requirements.txt
$ django-admin startproject portfolio .
$ ls -l
$ python manage.py migrate
$ sqlite3 db.sqlite3
sqlite> .tables
$ python manage.py createsuperuser
$ python manage.py runserver

Python Requirements
asgiref==3.4.1
Django==3.2.7
pytz==2021.3
sqlparse==0.4.2

00:00 In this short lesson, you’ll bootstrap an empty Django project that you’re going to host on Heroku. For a more in-depth explanation of the individual steps involved, check out Real Python’s course on setting up a Django project from scratch.

00:15 If you’re already familiar with Django and would like to use one of your own projects, then feel free to skip this lesson altogether. You can also download a sample project from other Django tutorials that are linked in the description below. For example, there’s one about building a personal diary in Django, which is still relatively straightforward but more interesting than a bare-bones Django project.

00:38 To begin, go to your terminal and make a new directory for your Django project. You can call it portfolio-project. Then, change your working directory to it and use Python’s built-in venv module to create an isolated virtual environment for your project dependencies.

00:56 It’s customary to place this virtual environment in your project’s root folder and call it venv. You may optionally define a descriptive name for your virtual environment to remind you which project it belongs to.

01:09 This will create a lightweight copy of your Python interpreter in the designated directory. To make this virtual environment active, however, you need to source a suitable shell script from the bin/ subdirectory.

01:22 If you’re using Bash, then source the first one by typing source venv/bin/activate. Your prompt should change by indicating the name of an active virtual environment. Now, when you try typing python or pip, it will point to the corresponding commands in your virtual environment.

01:43 Always make sure that you have activated the correct virtual environment before installing Django or any other dependencies. If you don’t provide a specific dependency version, then pip will install the most recent one. At the time of recording this lesson, the latest stable version of Django was 3.2.7, so you might want to explicitly request that version to avoid potential compatibility problems. When the installation completes, you may see a warning about a newer version of pip available.

02:13 It’s usually a good practice to keep your tools up to date for security reasons, so if that happens, then just copy and paste the suggested command to bring pip to the newest version. Even though you’ve only installed Django, the web framework came with its own set of dependencies, such as a WSGI server, a timezone management tool, or a SQL parsing library. To ensure reproducible builds of your project, you should always freeze your dependencies and save them in a requirements file. Without it, Heroku won’t be able to build and run your project.

02:49 Now it’s time to use the admin tool to scaffold the default directory structure, configuration files, and the management app for your Django project. You need to name your project using a valid Python module name because it has to be importable. This means no hyphens, no whitespace, nor special characters.

03:07 Other than that, watch out for choosing names that would conflict with Python keywords, builtins, or any library names like django. Let’s stick with portfolio and create the project in the current working directory.

03:22 That will create the management app, along with the management script that you can use to apply pending migrations against your database. By default, every new Django project is hooked up to a local file-based SQLite database named db.

03:37 You can inspect its contents using a command-line tool like SQLite3

03:43 and show the tables that Django has just created.

03:48 One of those tables is meant to store Django users and their credentials that you can log in with to the built-in Django admin interface. So if you intend to use the admin, then create a superuser.

04:02 Provide its name, the email address,

04:08 and password.

04:15 Finally, you can run a local development server that ships with Django and test your web application in the browser.

04:24 Hooray! It’s alive and working. The next step is to initialize a Git repository for your project, since you will need that to push the code to Heroku and trigger the build.

Become a Member to join the conversation.