Hey, welcome back.
In the previous video, we worked on the database model.
So if we run the program, the app now with Python Manage.py run server.
We should see this on our browser.
Now this is the home page.
So how can we display a custom home page here?
Well, we can do that using view functions and HTML view functions are.
Let me bring you the Flask app we developed in the previous sections.
So in our Flask app index, this index function was a view function.
So the index function was executed when the home page was visited.
Right?
And so the index function that then returns the index.html file so that the user can see the website
on their browser.
So Django view functions are similar to define a Django view function.
You want to locate your app directory job application in our case and then locate the views.py file
which was generated in the beginning when we executed the command python manage.py start app.
So you want to delete that comment and start creating your views with define followed by the name of
the function followed by a request argument.
So that's a parameter that the function expects from Django.
So when Django calls it Django passes a request on the background so you don't have to worry to call
this function right.
The function will be called when the user visits the URL here, and then the function will return an
HTML template.
Now with Flask, we use the render template function.
With Django, it's just render.
So make sure that render is imported here from Django dot shortcuts import render.
And this expects now the name of the HTML file which we haven't created yet to create this file.
Let me show you something first.
If you go to settings in your Mysite directory settings dot pi and you should be able to see the templates
variable here which defines a this key.
So it's a list of dictionaries and this dictionary has this dirs and here you can specify directory
names where you want to place your HTML files, but if you don't specify them, which I do recommend
to do so don't specify any names, just close this.
Don't change anything.
So if you don't specify them, then Django assumes that they are inside a template directory which should
be located inside your job application folder.
So inside your app.
So job application templates and inside templates you want to create an HTML file.
So go to html file, name this index.html and don't delete this.
I'm adding this to git.
I'm using git.
Don't delete these lines.
We will use this as a template for now.
So I want to show you a generic Django app first and then we create that job application form.
So in the body here I'll just write hello, or maybe use some H1 tags to wrap this around.
Like that.
So this is what we've got, right?
An index function which returns the rendered index.html template.
That's good so far.
Our boats, we don't have a decorator here like we had with the flask app.
Right?
With the Flask app.
We had this.
Decorator which attached this URL.
The home page to this function.
But in Django it doesn't work like that.
In Django, there's another way to connect a function.
A view function to a URL so that when the user visits that URL.
This function is executed.
To connect a function.
A view function to a URL, you should create another job application.
You should create a new urls.py file.
Add it to git.
If you're using git inside here you want to import from django dot urls import path and also from dot
import views.
So this is something from django from the library.
Right.
And this here is the views dot py file.
We are importing this file as a module inside this file.
So that works because views and urls.py are in the same directory under job application.
Right.
So then you want to define a url patterns variable.
Be careful here because you should use the exact name as you see here.
So URL patterns without spaces without underscores because Django expects will look for this variable
name here.
So then this will be a list.
I'll just split that to have multiple lines.
And then you call that path function, which takes three arguments.
This one would be like the subdirectory in the URL.
Perhaps you want it like an about page or and so on.
But since this is the home page, you just leave it an empty string and then you do views dot index.
So we imported views to this file and this file has an index function.
So basically we are saying here that the home page should be connected to this function and then you
want to give a name to this which will be used later by Django.
So you should give the same name as this function name, but as a string.
So that's a string.
That's not a string.
Right?
You should be able to tell the difference here.
So that's a list with one item for now.
So the item is the call of the path function.
Then there's one more thing we have to do regarding the URLs.
You want to collapse this to close this directory and then open the mysite directory and inside my sites
you will find a urls.py file.
So this was generated again by Django when we set up the project.
Now these are the URLs of the project.
Right?
The other URLs that py file contains the URL patterns of the app.
So our job application app could have one or more URLs.
Right.
And then in here.
So in this URL patterns list you want to go after the comma press enter and add a new URL pattern.
So this is again an empty string.
And then you say include and then you specify the name of your app, which is job underscore application
dot URLs.
So this comes as a string and it points to the urls.py file of the job application app.
So that URLs file which contains the patterns of the app and include is something we need to import.
So just go here.
Django dot urls import path comma include.
With that, we can go to this page reload and sometimes you might get errors and the errors will display
something like this.
To troubleshoot these errors, you should look at the exception location here, which tells you where
in what file the error is occurring.
So in my case it's views dot y line five index index is the name of the function.
So let's go to that line.
Views.
Line five.
So again, it's complaining that render is missing one required positional argument template name.
So render actually gets more than one argument.
So the first argument should be request this parameter.
So that parameter goes as the first argument of this call the render call.
Right.
So this error Django thinks that you specified index.html as the request parameter and then you didn't
specify the second parameter.
So it thinks that the template name is missing as a second parameter.
So please change that to this request and index.html.
Then go back to the page reload and this is what you should see.
If everything is working fine.
So that is the hello H1 heading, which we served through our index.html file.
So that's how you create a web page, a static web page with Django.
Next in line is we want to add the form here and then accept user data in the form, store those data
in the database, send an email to the user, and then finally create an admin interface to view the
data that has been submitted from the user.
So let's do that in the next video.
Thanks a lot for this.
See you later.