DEV Community

Cover image for Setting up your first Django Project
Ryan Glass
Ryan Glass

Posted on

Setting up your first Django Project

There's lots of frameworks out there and Django is probably one of the more difficult ones to learn. There's lots tutorials on how to setup Django like the documentation Django Tutorials but if you're like me and have ADHD. I tend to lose focus real quick when reading documentation. Use a screen reader they really help get that feedback your brain craves.

The Basics

Let's get started on the basics. If you didn't know, Django is a Python web framework. So if you don't have Python installed on your system head over to Python main webpage and download the interpreter for your respective system. You can check if python is installed on your system by running this command in your terminal. Make sure you're running Python3 as Python2 has been deprecated.

python3 --version
Enter fullscreen mode Exit fullscreen mode

Once Python is installed, you can now install Django using pip, which is Python's package installer. Open your terminal interface and run the command.

pip3 install django
Enter fullscreen mode Exit fullscreen mode

After installing Django, you can check if it's installed correctly by using the command. This command does two things -m flag which runs the library module as a script and then checks Django's version with the --version flag. Which should output some number of the Django version currently installed.

python3 -m Django --version
Enter fullscreen mode Exit fullscreen mode

if you already have Django installed make sure you upgrade to the latest package using the command.

pip3 install --upgrade Django
Enter fullscreen mode Exit fullscreen mode

WOW! That was a lot you learned how to install python and how to install Django on your system! Be proud you did it!

Django Project Creation

you can now finally create a new project with your new shiny framework Django. In your terminal, navigate to the directory where you want your project to be and run. I'm going to put this on my Desktop for simplicity. Run the command below to create a new folder on your desktop.

mkdir myfirstdjangoproject && cd myfirstdjangoproject
Enter fullscreen mode Exit fullscreen mode

This command does two things.

mkdir runs a command to create a directory in your current folder.

cd changes your directory to the directory you just created.

Finally we can run this command. What we're doing here is invoking Django-admin when the Django module is ran as a script. We then run a subcommand startproject which creates our project. Then at the end you have your project name.

django-admin startproject myfirstproject
Enter fullscreen mode Exit fullscreen mode

Navigate to your newly created myfirstproject folder using the change directory command.

cd myfirstproject
Enter fullscreen mode Exit fullscreen mode

Now you can finally run the command to show that Django has been setup correctly. Django comes with a built-in development server.

python3 manage.py runserver
Enter fullscreen mode Exit fullscreen mode

This command does one thing it executes the script that Django created when you created your project and then runs the subcommand runserver which runs Django's built-in server.

You should see output in the terminal indicating the server is running by default it runs on the localhost at port 8000. You can change this port if you want to, but we'll keep it as is. Head over to your Web browser and enter or copy and paste the address.

http://127.0.0.1:8000/
Enter fullscreen mode Exit fullscreen mode

You should see a welcome page from Django.

Woo, that was a lot! Django is all setup and you're ready to change the world now. To recap you've learnt how to setup and create a Django project and start the server using the runserver command.

Creating your first app

To create an app within your project execute this command in the terminal using if you follow the Django documentation you can name it polls app.

python manage.py startapp myappname
Enter fullscreen mode Exit fullscreen mode

By default, Django does not know where your application is at so make sure you add it inside of your settings.py file under INSTALLED_APPS.

INSTALLED_APPS = [
    'myappname',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles'
]
Enter fullscreen mode Exit fullscreen mode

if you do not do this then you won't be able to work with your application inside of Django.

About Django

Batteries Included
Django is Batteries Included meaning you have pretty much everything you need to get your app off of the ground there's a login system, you have a db connection, you have crud like operations out of the box. So you don't have to worry about setting any of that up.

Django uses a model-template-view (MTV) architectural pattern. This means in order to work with Django you'll need to create models, views, and templates. The models.py, views.py have been created for you when you used the startapp command, but the templates folder needs to be created by you inside of your project in order for Django to render any HTML content.

Inside of your models.py you'll see an empty file with an import statement at the top. Something like. This is how where you'll create your database schema.

from django.db import models

# Models go here

Enter fullscreen mode Exit fullscreen mode

By default, Django uses SQLite which is great for development but once you move to production try your hands at using a Database like PostgreSQL. Once you've created your database schema, run the commands.

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

These commands will invoke your manage.py script and will run two subcommands:

makemigrations - which is responsible for creating new migrations based on the changes you have made to your models.

migrate - which is responsible for applying and unapplying migrations.

Django comes with a built-in admin panel. You need to create a superuser to access it. Simply run the command.

python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

Follow the prompts to create the user.
Access the Admin Panel. Once created run the server using the runserver command. In your web browser navigate to.

http://127.0.0.1:8000/admin
Enter fullscreen mode Exit fullscreen mode

That's really it continue developing your application by adding more models, views, URLs, and templates as needed. You can always refer back to the Django documentation if you feel like this tutorial wasn't thourough enough on setting up your first Django project.

Top comments (0)