The Django Logic

From Grundy
Jump to navigation Jump to search


There are a lot of ways to install Django. Refer to this link to get instructions on installing Django on your system.

Set Up

I hope you have installed django by now. It’s time to get a brief idea about how things work with django.We’ll be making a website that could work as your navigator to various files you open oftenly to just brief you about how templates,views and url files work together to setup your website. So, let’s start making our first website.

In the terminal, go to the location where you would like to make your django project. My project’s name is ‘Basics’, so i’ll now run the command : django-admin startproject Basics

Error creating thumbnail: File missing

Press enter and you will see a folder at the location you have created your project. I have created one on the desktop. If you open it, you’ll see a set of files have been set up for us. To check that you have successfully set up this, in the terminal use the cd command and reach to the folder which has file and on your terminal run command: python runserver

Error creating thumbnail: File missing

Now copy the development server django has provided you and open it in your browser (In our case it is, ‘’). You’ll see that this page shows up.

Error creating thumbnail: File missing

Your Own Website

Now, it’s time to customize our own website. For now, just follow a few steps as I say and I'll explain it later about how it works.

First of all create a file in the folder which other files of your project like, etc. Your file structure will look like this.

Error creating thumbnail: File missing

Working on

Now do some changes to your file. It should look like this.


Working on

Next make the following changes to your file

Error creating thumbnail: File missing

Explanation of changes in the file: So, what we are doing is, we first created a url for our page. Since this is the first page of our website so i wanted the path to be blank so the first argument of the path function is just a blank (‘’)there’s no space inside it.(path is an in-built function in django) The second argument of the path function is a function of the file which decides what is supposed to happen when the user opens this path, the third argument is the name of the url by what we will call it when required.

Since, we are using a function defined in file inside our we need to import it.

Explanation of changes in the file: has just opened the page for us. What is to be done on the page will be commanded by index function in the file. So the function renders us to the navigator.html simply. But, it also passes the note list, if you want to carry any information to the html page you send it this way. This is also used when we pass login credentials to the login html file, this is just an example many more to explore.

Working on Django templates

But, wait a minute, where’s this navigator.html file!!!! I did not make it. Yes, we still need to make an html file, now here’s when django templates come to play. We keep all our html files inside this templates folder inside our project. You can name the folder anything but remember its name, cause when later i make changes in we’ll use it.

So, create a templates folder inside your project, add its name in the TEMPLATES inside and create navigator.html inside templates folder.

  • Error creating thumbnail: File missing
  • Error creating thumbnail: File missing

First image is the structure of files and the second image is of the file. I have highlighted the change for you.

Now, simply make the html file as you want. As I said I'm making a navigator for most frequently used files by me. I will simply do that. You are free to do anything.

Error creating thumbnail: File missing

Now save all your files and run python runserver command on your terminal and open the development server. Hey! You just launched your first website on the development server.

Error creating thumbnail: File missing

This webpage simply takes you to the links when you click on the items in the list.

But, did you notice a major miracle? I did not pass my name in the html file, But, the webpage has it!!! HOW?? This is the magic of django template tags, see the Template:Name inside the html file, this is the same name which was in the note list i had passed through the render function in the file.

So, yes we’re done. You just learned how to use, and templates via this very simple website. Now, since you are done with the basic concept you are ready to dive deeper and learn some many more interesting tools which will make your website better.

I encourage you to study about some topics which i think are easy and will make drastic changes to your website:

  • Instead of working with this default setup, make more apps inside your website to add more features to it. Okay, i’ll give you a hint to help you know what it is; go to the outermost ‘Basics’ folder in your terminal and write the command: django startapp accounts . this makes an app named accounts in your project. Now you can go ahead and explore how to use it
  • Inside the app you have just made, you can see a file. Go ahead and explore why we need to create Django models and how to use it.
  • Use the built-in usercreation form to make a signup page for your website.
  • What excites me the most about django is DYNAMIC pages, in simple words those are pages which are different for every user like the my profile page on facebook.
  • While running python runserver on the cmd you might have noticed that the terminal says something like unapplied migrations, go discover about it and migrate your model.

These points of exploration will open a vast flow of understanding about django to you and then, you’ll be ready to brainstorm your own head and come with a good website. It’s time to get started with it. All the best :)