Hello guys, welcome to learnvern.
In the last topic, we learned how to add models in the django admin panel, where we created a model and managed that particular model with django.
Also, we saw how to add data in the models.
How to make models particularly.. all those things we saw there in the last video. okay? Right?
Now let's move on to see how to load templates on the browser in django. okay?
Here, What you will do today is, work with an HTML page that you have to load on the browser. right? Clear?
First of all, how to create a template in django?
If you wish to create a template in django, then that particular template has to be created in your html file. okay?
Here, you should have basic knowledge about HTML, through which you will be able to create a particular page.
And whatever you will do in that will be counted as a Template.
In the beginning, we saw django is an MVT framework in the ‘django introduction’. okay?
Django follows MVT design patterns.
What does “to follow MVT design patterns” mean?
We saw models. right?
What did we see in the last video ?
I taught you to work with django and to integrate the models with the django admin panel. right?
If any of you have missed that video. Kindly watch the last video for clear vision.
Now What we need to do if we want to create a frontend i.e. a template in django ?
We need to write HTML codes and follow its particular structure to create a template which means then we can keep the template of django.
That means if you want to create any frontend in django then you are required to create a template, and that template is created in html.
Okay? Right? Clear?
I will show you in a different way what you need to do.
First of all there is a file structure to create a django template.
What is that?
You need to create a folder named ‘templates’ in the apps folder and in that template folder you need to create a .html file. okay? Right?
That particular .html file will work only when it will be in the template folder.
Hope you are able to understand me
After that, now when you have to load the template i.e. to run or to show on the browser.
So here, you have created models from MVT and i even told you about templates.
Now about ‘views’.
Views means V.
you need to create a function in views.py that will render your template on the browser. okay?
you cannot render your template on the browser without this function.
Render means to show that particular template i.e. particular HTML file on the browser.
So how to create that ?
you have to give one function that is def function_name and then in that pass a ‘request’ in bracket.
Then return render request app slash index.html.
What will this function do?
This function will render your html page i.e. the template which we created, on the browser.
Which means it will display it on the browser. okay?
how to do it?
Let’s do it practically.
So here I am creating a folder named ‘django template’.
First, we need to set up our project here.
so we will go there and open our cmd. okay?
Our cmd is open here.
Then First we need to create a virtual environment so what was the command?
“Python minus m venv my venv” right?
your virtual environment is created through this command. okay?
Now we will activate the virtual environment through this particular command. right?
So our virtual environment is activated.
How did we get to know?
In this way.
Well, I have already explained all this in the video where we installed django.
I have explained How to create a virtual environment, how to create a project, how to create an application…
I have explained all this in the last ‘project application’ video.
Those who are unaware about it, must watch that video to understand the particular way to follow these things, okay?
So we created this virtual environment.
Now we will install django here.
right?
So our django is installed. Okay?
Now first we need to create a django project.
So django dash admin start project.
And then I have taken the name of the project as “project” itself for your clearity. right?
So we created the project also.
Now what do we have to make?
We have to create applications.
So manage.py startapp.
So for the app, I have taken the name as “app” only so that it will be easy for you to understand.
Okay? And after this we have to open our VS code.
So our VS code is opened. right?
Here is our full structure.
Then What is the first thing that we have to do ?
Go in settings.py and install your app.
Here you will install your app.
“Application install in project” okay?
next go to urls.py.
here you have to include your application. okay?
So how will you do it?
Path and then keep the double inverted commas blank.
Why do we keep that blank?
Because we have to directly send this particular request of application into urls.py.
Right?
Next, include, app.urls in single inverted commas. okay?
I hope this much is clear.
Now we have to go to the folder of applications, right click on this.
Create a new file and give it a name “urls.py” okay?
Copy this much from urls.py of the project and paste here. okay?
After pasting here. You have to remove these things. Okay? Right? clear?
So here everything is set up now.
What did I tell you about creating a template?
Create a new folder here in the folder of the application with the name ‘templates’.
Guys remember “templates”.
Adding ‘s’ in the end is mandatory.
Without ‘s’ you cannot recognize your template.
Which means django will not recognize your template okay?
again you have to create a folder named ‘app’ inside templates.
Why? Because those particular templates follow this particular app.
In short they are made for this app okay? Then create a new file ‘index.html’ by right clicking on the app right?
So your html file is created okay?
Now you just have to write html here. Then if you do 5 here you will get this code here through VS code functionality intelligence.
Then here I will just write ‘Hello learnvern’ in h1 tag.
okay?
What happened? a template is created.
My one html file is created.
You cannot directly show this file on the browser okay?
In django, If you want to render any html file on the browser , you need to go here in views.py.
Here you will create your function that means to call that particular file, you will create a function in views.py.
How will you do it ? def suppose i am giving the name of the function as ‘index view’ okay?
Here I am passing a request right?
And here we have to write return render, here request app slash index .html.
The app we wrote refers to the app in templates.
Okay? Now always remember one thing whenever you are creating this particular view.
Which view is this ours? This is our index.html view okay? Right?
You have to give one path in urls.py in application to call this particular view.
That means to define url why? Because your view will be called only through url.
So how to do it? First you need to import the views.py package.
Here ‘from.Import views’.
Here are our views. Next is to write a path and leave the first bracket of double inverted commas blank.
And here views.index view that is the name of the function and then name = suppose index. right?
What happened is, a url was created.
Now what will you do first?
Migrate your server that is in your cmd.
Migrate django files and then run the server.
What was the command? Run the server and then go to the browser and then call your ‘127 link’.
In what way? As soon as I enter here, the template is called right?
So in this way you will call your particular template.
Always remember, you must follow this file structure at the time of creating a template.
Otherwise your template will never render on the browser.
So please guys it is compulsory to follow this particular file structure in django. Right? Clear?
So I hope you are clear with all the things about how we did we render our template today and if you have any doubts, you can go to website forums.learnvern.com and can ask your questions topic wise.
And if you wish to download any file, there you will find your files in description. Okay?
In the next topic we will see how to create forms of a particular template in html.
Means we will create a template in django and see how to create a form there.
Let's meet in the next topic.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Share a personalized message with your friends.