Hello internet people, so we are starting, Learn Django by Doing Project series. So, our first project is Todo App in Django.
Today we install Django and setup the project. So let’s start. Before starting the tutorial first see a brief introduction of Django.
Brief Intro of Django
Django runs on an MVT (Model View Template) system which is different from MVC (Model View Controller) that powers Laravel.
Model: The model in Django sets out the schema for our database. With Django’s ORM, you can declare the fields, field types and any extra data for instance Meta information.
View: The View in Django, is where you set all of your code logic and algorithms. With the View, you can get some results from the database or manipulate some data. The view basically expects a request and a response. The response is usually a HTTP redirect, HTTP error (404), MimeTypes, or a call to load a template.
Template: The template in Django is the plain HTML code with Django’s Template Language in it. Django Template Language (DTL) is the language you’ll be using to dynamically write your template. You can do almost everything you’d with python and any other language.
Settings: The settings file in Django, holds all the settings of your web app. It includes the secret key, templates directories, middlewares (security), static files (css,js), database settings and so on.
Url: It helps to connect the view to a url.
Admin: This deals with how you want to view your models in the Django admin.
Virtual Environment Setup
Virtualenv is a tool used to create an isolated Python environment. This environment has its own installation directories that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).
pip3 install virtualenv
Before making a virtual environment we make project directory. Inside it, we make a virtual environment.
Inside ToDoAppProject directory we setup virtual environment.
First find your python installation path of particular version,
/ToDoAppProject$ which python3.8
So here I get python3.8 path. And I want python3.8 in my virtual environment.
/ToDoAppProject$ virtualenv -p /usr/bin/python3.8 env_todoapp
virtualenv command we created env_todoapp virtual environment. Every python installation goes inside that. But before installing python stuff we need to activate the virtual environment.
/ToDoAppProject$ source env_todoapp/bin/activate
Now your terminal look like this.
No, we are ready to install Django inside virtual environment. So let’s go…
I am installing Django 3.1.1 here. Install whatever the new version release.
pip3 install Django==3.1.1
We are in the ToDoAppProject directory okay and the virtual environment is activated. So, all installation is done inside our env_todoapp.
Creating todoapp Project
Everything going fine right. Now we start the project using django-admin.
django-admin startproject todoapp
The command should have created a todoapp directory. Use the command cd todoapp to go into the directory.
Run following command inside todoapp directory.
/todoapp$ python3 manage.py runserver
Once you’ve created the project and run this command, you can run it and view the URL in the browser. You can do that with the command above and visit the given URL:
You should be able to see something like below
If you see this, congratulation!! your Django installation is done successfully. Now we create an app.
Creating todolist App
After that, you’ll need to create a django app that holds the model and admin stuff. You can do that with the command below:
python3 manage.py startapp todolist
Open todoapp directory in your favorite code editor. I am using VS code. Below is the structure of how our project file structure looks like after setting up everything:
Now open settings.py in the todoapp directory. The first thing you need to do is to add the created app “todolist” in the INSTALLED_APPS.
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'todolist', ]
This holds the Django apps you’ll be using in the Django project.
That’s it for this article, next we will make model for storing task, we learn about migrations and django admin panel.
Hope you like it. Please share this tutorial with your friends.