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 venv 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 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-7 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