image
Introduction to Django (Part 5)
#Python #Programming #Beginner #Django

Templates

Templates in django are responsible for outputting our intended data. It's the frontend of our system, for in templates you can use any frontend framework you desire.
In this articles we will get into templates and we will be using bootstrap 4 and html.
First we need to create a templates folder in the root directory, where we have manage.py and we'll name the folder templates. 
Now all our html files will be saved inside the templates folder, and we will start by creating our base html.

Base html will be like our main html file, in the base html we will link our stylesheets and javascript and have any code that will shared across all our other pages for example, the header and the footer.

This helps in increase load time speeds since all the pages will sharing on main file which in our case is base.html.
Next we need to create our index.html file. We can choose to create another folder specific to our app name inside our templates folder or maybe we just create our index.html alongside base.html. Either way it will work.
So in our case, let's create another folder and name it blog as our app name. Then inside our blog folder we'll create our index.html file, this is just normal html file. 

After having that now we need to tell our project where to find the templates folder so we'll open our settings.py and locate TEMPLATES and add this code:

TEMPLATES = [
    {
        ......
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
       .....
    },
]

This will now tell where we have our templates file. And they can be displayed when called.

Views

Doing that is not enough yet now we need to go our views.py and set our views. For example if we want to just render the index.html without fetching anything from the database we'll use the code as below:

def index(request):
    return render(request, 'blogs/index.html', {})


In other ways if we want to display something from the database we can we use this:

def index(request):
    blogs = BlogPost.objects.filter(status=1)
    template_name = 'blogs/index.html'
    return render(request, template_name, {'blogs': blogs})


Here we are declaring what template name we want to view, then we pass a query to fetch whatever we want to display from the database then pass it our return function.
Displaying something from the database will again require us to edit our index.html file so that we set which part of the page we need our information displayed by using jinja tags as:

<h1 class="feature-title"><b>{{ blog.title }}</b></h1>


We are using blog.title, blog is the name we used to declare our for loop then the title is the name of the column we used in our database. 
Note: Make sure when using jinja tags always remember to close the tags, at times I tend to forget that.

Urls

Now we need to set our URL which leads to our views which in turn loads our templates. We now need to create a urls.py file in our app folder if there wasn't such a file in our case in our blogs folder and have this:

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]


We are importing our views then we pass the path.
We used empty quotes ' ' so that our home url would load up the index.html file then we pass our views name. After having this, we need  now to import this urls.py file into our main urls.py file in our project folder which is alongside the settings.py by having this:

from django.urls import path, include
.....

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blogs.urls'))
]

 
This will import our app's urls.py file and the pages can now load up well without errors.

That's all I had for you in this article, next article we'll get into configuring our static files i.e CSS, JS, images so that our index.html file can have some styling and look attractive.
See you soon, don't forget to hit that upvote button if it helped...