So far we have understood the basics of django framework. Now, we will create a simple blog application which will fetch json data from a server and then parse the blog data to show blog posts.

Let’s go ahead and make a new app, calling it personal, since this is our personal website.

python manage.py startapp personal
Capture

So we’ve added an app, and we of course fully intend to use it, so what do we need to do? Install! Heading to mysite/settings.py, find INSTALLED_APPS

INSTALLED_APPS = [
    'personal'
    'webapp',   
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

 

Capture

 

You can delete our webapp app which was just for simple showing.
But we will not delete for the time being.
Next, we need to actually point to this new personal app. Open mysite/urls.py:

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', include('personal.urls')),

    #Here, we're adding a new url pattern '^$'
    # which just quite easily means a pattern that starts, and end
    # this will be the "index" or "home" page of this site
    # for example - http://127.0.0.1:8000
]

Capture.PNG

So, guess what to do now?
Exactly, we need to go to personal/urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$', views.index, name='index')]

Capture.PNG

Next ?
Head to personal/views.py:

from django.shortcuts import render

def index(request):
    return render(request, 'personal/home.html')

Render takes the request parameter first, then the template to render, then an optional dictionary of variables to pass through to the template. You can learn more about render from here .
For now, we keep it simple. We just simply want to load a home.html template.
But we don’t have home.html yet , so let’s create home.html file.
But here ??
Here there are some django specific rules. All static files should be under a folder named exactly “templates ” and each individual app should have it’s own “templates” folder.
All though this default setting can be changed via setting.py file. we will discuss this latter. But for the simplicity we will stick to default behavior.

Generally, websites look very similar on their various pages, because they follow a template, usually referred to as a header. The header contains things like the navigation bar, maybe a banner image, and so on. This header stays the same, and should be contained in one main file. Imagine if you had a website with 100 unique pages, each containing the code for the navigation bar, and you wanted to change a button on it. You would have to edit 100 files, and that’s very tedious! Thus, we have templates, like headers. That said, with Django, you’re going to have headers as templates, and then other templates that “extend” these headers and act as parts.

So, for our personal app we will create a folder named “templates ” to keep all the html template files. Then we will also create another folder same name as the app name . There are lot more discussion for this but we want to make it simple for understanding the django as implest form.
Django is going to search for the template by name. You may have multiple header templates, maybe multiple “home” templates for various “homes” of the apps, and so on. To make sure you don’t accidentally wind up conflicting, you add the app’s name, so it’s a part of the dynamic reference. Thus, the path starting from the root project, mysite, we have directories: mysite/personal/templates/personal.
So , for our personal app create a folder structure like below:
Capture.PNG

Create a header.html file

<!DOCTYPE html>
<html lang="en">

<head>
	<title>django</title>
	<meta charset="utf-8" />
</head>

<body class="body" style="background-color:#f6f6f6">
	
	   {% block content %}
             #some dynamic content will be replaced here
	   {% endblock %}	
</body>

</html>
       	   {% block content %}            
	   {% endblock %}	

This code block is not Django! It’s not really Python either. A wild Jinja has appeared, Jinja2 specifically. Jinja is a template engine for the Python programming language, aimed at helping you to do dynamic things with your HTML like passing variables, running simple logic, and more! With Jinja, you will notice we are using {% %}, this denotes logic. For variables, you will see {%{% }}. The use of block content and endlock is one of the two major ways of incorporating templates within templates. This method is generally used for dynamic templeting. Dynamic contents will be replaced in this block from another python file which will be passed. To know more about django templeting goto this link .

Now create a home.html file:
Capture.PNG

personal/templates/personal/home.html

{% extends "personal/header.html" %}
{% block content %}
<p>Hey!! i am from home.</p>
{% endblock %}

Now , when a python file call home.html from any view , first thing will be executed is that
{% extends “personal/header.html” %} 
It will just extend the header.html for reusing and below content will be replaced in the dynamic block content of header.html file

# this content will be replaced inside the header.html file 
{% block content %}
<p>Hey!! i am from home.</p>
{% endblock %}

 

Now if we run this so far we will get the below result
Capture.PNG

 

PART 5