Getting started with Django

Part 1: Django Series

Anurag
6 min readJan 25, 2023

In this article series on Django, we will learn how to use Django to build a fully functional website. We’ll cover things like rendering your first page setting up a database adding items to that database and rendering those items out on a web page of the application. We will try to build a customer management platform for this learning purpose. The application we create will store customer information in a database like a CRM.

Users will have the following abilities:

  1. To create customers and customer orders.
  2. Viewing those customers and updating customer information like orders and basic contact information on a customer’s profile page.
  3. We’ll also have the ability to search customer information using a multi-parameter search form.
  4. We will also be adding login authentication and password resetting.

So, what is Django? To summarize, Django is a way we can create websites using Python. It’s a very popular web framework that makes putting together websites much easier rather than writing out all that Python code yourself. So let’s get started by installing Django and getting a basic project setup.

Installation of Django

Let us start with making a folder for our required project. I have made a folder with the name “DjangoTutorial” in my case. Now let’s create a “virtual environment” for our project. In case you are not aware of how to make a virtual environment for your project, you can go through my article mentioned below.

https://nowitsanurag.medium.com/creating-python-virtual-environment-in-linux-fd1222ee9194

We’ll first need to install Python because we are downloading django using the pip command. So I’m in the directory where I want to host my project and I’ll just go ahead and run the command

pip install django
Installing Django using the pip command

To make our first project, run the below command and give your project a name. In this case, I have named it “CRM”.

django-admin.py startproject crm
Making the first project with the name CRM

Now let’s get inside the directory that we just created. Use the below command to enter the folder.

cd crm
Our project destination

For this tutorial purpose, we will be using VS code. In case you want to download it, follow this link. You can use any other editor of your choice.
Now let’s take a look inside the crm folder.

manage.py

manage.py

You never want to touch this file. This file gives us access to just a list of commands we’re going to use very frequently. So we never want to touch it.

wsgi.py

wsgi.py

We have this WSGI file, a web server that Django sets up for us, and we’re also not going to touch it.

urls.py

urls.py

We have this file that is just a URL routing system. It is just a list and we’re gonna add a list of paths.

settings.py

settings.py

This is the main file that we’re gonna be using. This python file is like the command center or the main configuration of your django app. You see in the above image, we have the database setup. Any time we are installing or changing up our database, the main configuration all happens here.

Installed apps of settings.py

When you want django to know about a new install, we’ll usually add it to this list (see the screenshot mentioned above).

So for now okay so now that I cover the file structure, let’s see how to launch a basic django app and see what happens when we turn on our server. We will use the “manage.py” file for this purpose and in the command prompt in our root directory of the django project, make sure we’re in there. Run the following command.

python manage.py runserver
Running the server

Once you see a message like the above image, it means your server is on and you can now see your project on port 8000 (http://127.0.0.1:8000).

The image shown above is the basic setup that django gives us; we have access to two different pages right now. The image shown above is the base template and if you look at our URL path, we also have access to this
admin page (http://127.0.0.1:8000/admin).

We can turn off our server by clicking ctrl+ C and again we can
turn it back on by doing run server and activating it.

So now that I covered the basic file structures of django, we need
to talk about the concept of apps within our django project. Let’s take the concept of Facebook and see how they would structure their projects.

So Facebook is our base project, that’s the django application we just set up. Facebook is comprised of multiple sections so they have things like profile pages, they have things like newsfeeds, and groups. These in this example would be different apps within the entire Facebook project. A good way to think about apps is if you can’t explain what that file does in one sentence, it needs to be its app and this is where we can separate our database files. So we’re not coming up with massive file sizes and then creating a bunch of bugs later. We are gonna only create one app for learning purposes.

Let’s go ahead and make one in our project. I’ll go ahead and turn off my server and create our new app. Run the following command to make a new app in the same project.

python manage.py startapp accounts
New app “accounts” in the same project “crm”

You’ll see the accounts folder appear in your directory and it just gives us another set of files we can start working within. I’ll just go ahead and run down that structure briefly, just like I did with the initial project.

The main files in the accounts folder we need to focus on here are gonna be the following:

admin.py

We’ll cover this once we start setting up the admin panel.

admin.py

models.py

This is going to be where we build our database in class-based objects and it’ll represent the file structure that we’re gonna have in our database.

models.py

views.py

These are going to be the functions or classes actually that activate that our URL patterns go to and trigger to actually render out a template and so on.

views.py

Now, we’ll need to configure our app and let our project know to be aware of the new app we’re now connected to. Follow the below procedure to do the process.

Go to settings.py and in INSTALLED_APPS, add it as a list item you just need to give it the name of your app. In this case, we have named them as accounts. Once we have that, django now knows that we have this project or this folder within it.

Hope you get some insights into Django from this article. See you again in another blog.

--

--

Anurag

Currently working as Product Manager who is also a passionate engineer with an experience in Artificial Intelligence.