Senin, 28 Juli 2014

Using Templates in Django | Python in Web development - Part 3

In previous part of this tutorial series, we show how you can create simple web application using Django. how to develop basic and very first Website using Django.

In this post we are going to see ho we can use Templates in Django to render HTML code on Web page. Initially we hard coded the HTML part in views (views.py)

Let's start with very basic idea about how template works. Goto the directory you created in previous part(or any other Django project) and run python manage.py shell (It is necessary that you open terminal using this command)

Run the following commands,

What we have done is,
  1. Imported template module.
  2. Created template using Template() method. Everything inside the '{{}}' are variables. In our example we have two variables name and age.
  3. To assign value to this variable, we used Context() method, which takes python dictionary as argument.
  4. Now we render the template using render() method. It takes template.Context as argument and return Unicode String. You can render Template with various Context.

Now to use this in Project. you can write following code in your views.py.

views.py
from django.http import HttpResponse
from django.template import Template, Context

def first_project(request):
return HttpResponse("<h3>Welcome to my First Project</h3>")

def hello(request):
list_books = ['one','two','three']
html = """
<html>
<head><title>Homepage | Ronak khunt</title></head>
<body>
<h1>Welcome {{ uname }}</h1>
<ol>
{% for book in list_books %}
<li>{{book}} ell</li>
{% endfor %}
</ol>
</body></html>
"""
t = Template(html)
c = Context({"uname":'Ronak','list_books':list_books})
return HttpResponse(t.render(c))

Open your urls.py and set URL for this(hello()) method/View. Also note the use of for loop. Using template does not solve the problem of hard coded HTML.

To solve this problem Django provides get_template() method. Before using this method you have set TEMPLATE_DIRS in your settings.py. Open settings.py and add path to directory in which you want to store your HTML files.

settings.py
......
......
#Don't forget trailing comma at the end.

TEMPLATE_DIRS = (
'/home/user/django/first_project/templates',
)
......
......

Now create hello.html file in template directory(you have to create this Dir.) and write any code in it. In our case we will write following code.

hello.html
<html>
<head><title>Homepage | Ronak khunt</title></head>
<body>
<h1>Welcome {{ uname }}</h1>
<ol>
{% for book in list_books %}
<li>{{book}} ell</li>
{% endfor %}
</ol>
</body>
</html>

Now open views.py file of your project and add following method.

views.py
#import get_template
from template.loader import get_template
#other import statement
...

def first_project(request):
...
def hello(request):
...

def hello2(request):
t = get_template('hello.html')
c = Context({"uname":'Ronak','list_books':list_books})
html = t.render(c)
return HttpResponse(html)

Open your urls.py and set URL for this(hello2()) method/View.

Django also provides shortcut for this. You can use following method as shortcut.

views.py
#import render()
from django.shortcuts import render
#other import statement
...

def first_project(request):
...
def hello(request):
...
def hello2(request):
...

def hello3(request):
return render(request, 'hello.html',
{"uname":'Ronak','list_books':list_books})

We will also set URL for this(hello3()) View. at the your urls.py will look someting like this.

urls.py
from django.conf.urls.defaults import patterns, include, url
from firstproject.views import first_project, hello, hello2, hello3

urlpatterns = patterns('',

url(r'^first_project/$',first_project),
url(r'^hello/$',hello),
url(r'^hello2/$',hello2),
url(r'^hello3/$',hello3),

)

That is all about using template in Django.

Was this Information helpful?

Yes No


Tidak ada komentar:

Posting Komentar