About me

Wednesday, May 16, 2018

Python django chap 7 (Templates)

Friends this is the continuation from its previous post so i kindly recommend you to read that post i have made this very simple even a complete beginner can learn it and if you are beginner in python then you need to learn python i have made that blog also for that click this link



TEMPLATE NAMESPACING
Now we might be able to get away with putting our templates directly in myproj/templates (rather than creating another myproj subdirectory), but it would actually be a bad idea. Django will choose the first template it finds whose name matches, and if you had a template with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those templates inside another directory named for the application itself.

TEMPLATES 

Lets start with template as myproj/index.html

<html>
   <body>
 
      Hello World!!!Today is {{today}}<br />

      We are
      {% if today.day == 1 %}
     
      the first day of month.
      {% elif today.day == 30 %}
     
      the last day of month.
      {% else %}
     
      I don't know.
      {%endif%}
     
   </body>
</html>
Just like in Python you can use if, else and elif in your template 

Render Function

This is the method by how the template is used in the view The Render function takes three parameters which are
Request :The initial request.
Path to the Template:This is the path relative to the TEMPLATE_DIRS option in the project settings.py variables.
Dictionary of Parameters:A dictionary that contains all variables needed in the template. This variable can be created or you can use locals() to pass all local variable declared in the view.

fill a context and return an HttpResponse object with the result of the rendered template. Django provides a shortcut.


from django.shortcuts import render


 
def index(request):
    today = datetime.datetime.now().date()
    
    return render(request, 'myproj/index.html',{ "today",today })


The code given above for the views will give the date to the template view and the template view will get the day from the date and display the result according to the day


BLOCK AND EXTEND TAGS
Here we are going to see about the template ingeritance the method in which the when you are designing your templates, you should have a main template with holes that the child's template will fill according to his own need, like a page might need a special css for the selected tab.Let’s change the index.html template to inherit from a alter_template.html.

<html>
   <head>
     
      <title>
         {% block title %}Page Title{% endblock %}
      </title>
     
   </head>
   
   <body>
  
      {% block content %}
         Body content
      {% endblock %}
     
   </body>
</html>
And now this is our myproj/index.html

{% extends "alter_template.html" %}
{% block title %}My Hello Page{% endblock %}
{% block content %}

Hello World!!!<p>Today is {{today}}</p>
We are
{% if today.day == 1 %}

the first day of month.
{% elif today.day == 30 %}

the last day of month.
{% else %}

I don't know.
{%endif%}

<p>
   {% for day in days_of_week %}
   {{day}}
</p>

{% endfor %}
{% endblock %}
 
In the alter_template.html we define blocks using the tag block. The title block will contain the page title and the content block will have the page main content. In index.html we use extends to inherit from the alter_template.html then we fill the block define above (content and title).


Namespacing URL names


 The tutorial project has just one app, myproj. In real Django projects, there might be five, ten, twenty apps or more.  Django for differentiating the URL names between them add namespaces to your URLconf. In the myproj/urls.py file, go ahead and add an app_name to set the application namespace
app_name = 'myproj'
Hope you understand the part the rest is on the next post


THANKS FOR VIEWING MY BLOG HOPE YOU LIKE THE POST IF YOU WANT TO GET LATEST POSTS ON THIS PLEASE FOLLOW BY EMAIL ID AND FEEL FREE TO ASK YOUR DOUBTS AND DON'T FORGET TO SHARE,COMMENT AND FOLLOW  

No comments:

Post a Comment