The Django Logic
There are a lot of ways to install Django. Refer to this link to get instructions on installing Django on your system.
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
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 manage.py file and on your terminal run command: python manage.py runserver
Now copy the development server django has provided you and open it in your browser (In our case it is, ‘http://127.0.0.1:8000/’). You’ll see that this page shows up.
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 views.py file in the folder which other files of your project like urls.py, settings.py etc. Your file structure will look like this.
Working on urls.py
Now do some changes to your urls.py file. It should look like this.
Working on views.py
Next make the following changes to your views.py file
Explanation of changes in the urls.py 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 views.py 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 views.py file inside our urls.py we need to import it.
Explanation of changes in the views.py file: Urls.py has just opened the page for us. What is to be done on the page will be commanded by index function in the views.py 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 settings.py we’ll use it.
So, create a templates folder inside your project, add its name in the TEMPLATES inside settings.py 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 settings.py 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.
Now save all your files and run python manage.py runserver command on your terminal and open the development server. Hey! You just launched your first website on the development server.
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 views.py file.
So, yes we’re done. You just learned how to use urls.py,views.py 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 models.py 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 manage.py 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 :)