How to Create a Django Project in 5 Minutes

March 08, 2021

Getting started with Django is super fast and easy!

Known as “the web framework for perfectionists with deadlines,” Django powers some of the biggest websites in the world.

I write about Django a lot, and this post is my ultimate resource for getting started with Django from scratch.

Steps to Get Started with Django

  1. Create a virtual environment (optional, but highly recommended)
  2. Install Django
  3. Create a project

For the Visual Learners

Here’s a video of me doing these steps for a project:

1.1 Virtual Environment

First, consider creating a new virtual environment for your project so you can manage your dependencies separately.

I use pyenv and pyenv-virtualenv for my environments:

$ pyenv virtualenv django-tutorial

Looking in links: /tmp/tmpjizkdypnRequirement already satisfied: setuptools in /home/bennett/.pyenv/versions/3.6.8/envs/django-tutorial/lib/python3.6/site-packages (40.6.2)Requirement already satisfied: pip in /home/bennett/.pyenv/versions/3.6.8/envs/django-tutorial/lib/python3.6/site-packages (18.1)$ pyenv local django-tutorial

1.2 Install Django

Now, we can install Django:

$ pip install django

Next, let’s start a new Django project:

$ django-admin startproject mysite

If we look at the directory now, we’ll see that Django created a new folder for us:

$ ls mysite/

And if we look inside that folder, there’s everything we need to run a Django site:

$ cd mysite/ $ ls manage.py* mysite/

Let’s make sure it works. Test run the Django server:

$ python manage.py runserver

Watching for file changes with StatReloader Performing system checks...

System check identified no issues (0 silenced).

You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them.

May 17, 2019 - 16:09:28 Django version 2.2.1, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

Go to localhost:8000 and you should see the Django welcome screen!

Hey! Django works!

1.3 Create app

We could build our application with the folder structure the way it is right now. However, best practice is to separate your Django project into separate apps when you build something new.

So, let’s create a new app for our logic:

$ python manage.py startapp myapp $ ls db.sqlite3 manage.py* myapp/ mysite/

1.4 Register the myapp app with the mysite project

We need to tell Django to recognize this new app that we just created. The steps we do later won’t work if Django doesn’t know about myapp.

So, we edit mysite/settings.py :

INSTALLED_APPS = [ 'myapp.apps.MyappConfig', ... # Leave all the other INSTALLED_APPS ]

1.5 Migrate the database

Remember how I said Django allows you to define database models using Python?

Whenever we create or make changes to a model, we need to tell Django to migrate those changes to the database. The Django ORM then writes all the SQL CREATE TABLE commands for us.

It turns out that Django comes with a few models already built in. We need to migrate those built in models to our database.

(For those of you thinking, “We didn’t create a database!” You’re right. But Django will create a simple SQLite database for us if we don’t specify differently. And SQLite is awesome!)

So, let’s migrate those initial models:

$ python manage.py migrate

Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying auth.0010_alter_group_name_max_length... OK Applying auth.0011_update_proxy_permissions... OK Applying sessions.0001_initial... OK

1.6 Create Super User

One more thing before we move on.

We’re about to create some models. It would be nice if we had access to Django’s pretty admin interface when we want to review the data in our database.

To do so, we’ll need login credentials. So, let’s make ourselves the owners and administrators of this project. THE ALL-POWERFUL SUPERUSER!!!

$ python manage.py createsuperuser

Username (leave blank to use 'bennett'): Email address: hello@bennettgarner.com Password: Password (again): Superuser created successfully.

Let’s verify that it works. Start up the Django server:

$ python manage.py runserver

And then navigate to localhost:8000/admin

Oooo, Django Admin!!! Pretty.

Log in with your superuser credentials, and you should see the admin dashboard:

Look at those lovely User and Group models that we just migrated!

We did it!

It’s really that easy to get started with Django!

You now have the basic file structure to build anything. Add views and models to your app to start creating features.

Django makes it easy to build complex sites that solve problems for people. If you’re new to Django, I’m excited to welcome you into this ecosystem. Here are some cool next steps you can take:

Build your first REST API with Django REST Framework _Building a REST API in Django is so super easy. In this tutorial, we’ll walk through the steps to get your first API up…_medium.com

Celery Tutorial: A Must-Learn Technology for Python Developers _Workers to process tasks in the background are essential & powerful tools in any developer’s toolkit._medium.com

React on Django: Getting started _Here’s my quickstart guide for getting up and running with React on a Django backend._blog.usejournal.com

Have fun coding, and welcome once again to the world of Django!

Like what you’ve read here?

I share my best content with my email list for free.

Join 500 other developers currently in my email series.


Profile picture

I write something new every day for 2k software developers. You should sign up for the daily email.

© 2024