Your First Steps With Django: Set Up a Django Project
At the end this article,you’ll know how to:
- Set up a virtual environment
- Install Django
- Pin your project dependencies
- Set up a Django project
- Start a Django app
Setup Virtual environment
When you’re ready to start your new Django web application, create a new folder and navigate into it. In this folder, you’ll set up a new virtual environment using your command line:
Step-1 Make a Folder
$ mkdir djangoProject
Step-2 Navigate into it
$ cd djangoProject
Step-3 Setup Virtual Environment
$ python -m virtualenv django-env
This command sets up a new virtual environment named
django-env in your current working directory. Once the process is complete, you also need to activate the virtual environment:
Step-4 Activate Virtual Environment
If the activation was successful, then you’ll see the name of your virtual environment,
(, at the beginning of your command prompt. This means that your environment setup is complete.
Step-5 Install Django and Pin Your Dependencies
Once you’ve created and activated your Python virtual environment, you can install Django into this dedicated development workspace:
For python 3
(django-env) $ python -m pip install django
For python 2.7
(django-env) $ python -m pip install django==1.11.22
This command fetches the django package from the Python Package Index (PyPI) using
pip. After the installation has completed, you can pin your dependencies to make sure that you’re keeping track of which Django version you installed:
Step-6 Pin your dependencies
(django-env) $ python -m pip freeze > requirements.txt
This command writes the names and versions of all external Python packages that are currently in your virtual environment to a file called
requirements.txt. This file will include the
django package and all of its dependencies.
You should always include a record of the versions of all packages you used in your project code, such as in a requirements.txt file. The requirements.txt file allows you and other programmers to reproduce the exact conditions of your project build.
Suppose you’re working on an existing project with its dependencies already pinned in a requirements.txt file. In that case, you can install the right Django version as well as all the other necessary packages in a single command:
(django-env) $ python -m pip install -r requirements.txt
The command reads all names and versions of the pinned packages from your requirements.txt file and installs the specified version of each package in your virtual environment.
Keeping a separate virtual environment for every project allows you to work with different versions of Django for different web application projects. Pinning the dependencies with pip freeze enables you to reproduce the environment that you need for the project to work as expected.
Step-7 Set Up a Django Project
After you’ve successfully installed Django, you’re ready to create the scaffolding for your new web application. The Django framework distinguishes between projects and apps:
- A Django project is a high-level unit of organization that contains logic that governs your whole web application. Each project can contain multiple apps.
- A Django app is a lower-level unit of your web application. You can have zero to many apps in a project, and you’ll usually have at least one app.
With your virtual environment set up and activated and Django installed, you can now create a project:
(django-env) $ python -m django-admin startproject <project-name>
This article uses setup as an example for the project name:
(django-env) $ python -m django-admin startproject setup
Running this command creates a default folder structure, which includes some Python files and your management app that has the same name as your project:
setup/ │ ├── setup/ │ ├── __init__.py │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py │ └── manage.py
In the code block above, you can see the folder structure that the startproject command created for you:
- setup/ is your top-level project folder.
- setup/setup/ is your lower-level folder that represents your management app.
- manage.py is a Python file that serves as the command center of your project. It does the same as the django-admin command-line utility.
The nested setup/setup/ folder contains a couple more files that you’ll edit when you work on your web application.
Step-8 Start a Django App
You don’t need to use the django-admin command-line utility anymore, and you can execute the startapp command through the manage.py file instead:
(django-env) $ python manage.py startapp <appname>
The startapp command generates a default folder structure for a Django app. This tutorial uses
example as the name for the app:
(django-env) $ python manage.py startapp example
Remember to replace example with your app name when you create the Django app for your personal web application.
Once the startapp command has finished execution, you’ll see that Django has added another folder to your folder structure:
setup/ │ ├── example/ │ │ │ ├── migrations/ │ │ └── __init__.py │ │ │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── models.py │ ├── tests.py │ └── views.py │ ├── setup/ │ ├── __init__.py │ ├── asgi.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py │ └── manage.py
Step-9 Run Server
$ python manage.py runserver