DEV Community

Nehama Mandelbaum
Nehama Mandelbaum

Posted on • Edited on

How to start a Django project

Hello!
In this post I'll show you how to start a project using python and Django.

For this tutorial, you have to have python already installed in your computer.

Step 1: Create a directory for your project and enter it:

mkdir project_name
cd project_name
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a virtual environment in your project folder:
Unlike node.js, where the node_modules folder is created automatically when you run the command to start a project (yarn init or npm init), when using python and django, to have your project dependencies installed locally in your project directory instead of globally in your computer, you have to create your own virtual environment from scratch.

python -m venv venv

Enter fullscreen mode Exit fullscreen mode

Step 3: Activate your virtual environment:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Step 4: Install the basic dependencies:

pip install djangorestframework black ipython
Enter fullscreen mode Exit fullscreen mode

I like using djangorestframework because it makes rest API development so much easier, and already installs django automatically.

Step 5: Create a file named requirements.txt, so that pip knows which dependencies to install if you run your project in another machine:

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

Step 6: Start your project;

django-admin startproject name_of_your_project .
Enter fullscreen mode Exit fullscreen mode

Since we already prepared the environment in our directory, I like to use the dot in the end of the start project command, to tell django to start the app in my current folder. If I don't use it, django makes another directory inside my current directory, which in our case is unnecessary.

Extra tip: Don't forget to create a gitignore, and put the venv directory in it, as well as the pycache, and your db.sqlite3 (once your run your migrations, django automatically generates a file named db.sqlite3, which is NOT recommended to upload to git)

That's it for today, I hope I helped you in some small way!

Top comments (0)