10. Working with templates in django

from the above we got the html response ....... that is fine ......


but for multiples pages we need to go for templates(acutally templates will have html,css and etc.... files )



let we will do this with template folder in that some html files......




ok for this we need

template folder in app(login)
html files in template folder.




so let us start it.....


first create  templates folder

and give the dir link in the

setting.py


TEMPLATE_DEBUG = True

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\', '/'),
)

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)




home.html(in templates folder)


<html>
<body>
hello world
</body>

</html>






view.py


from django.shortcuts import render_to_response
from django.http import HttpResponse
import datetime

# Create your views here.


def home(request):
    return render_to_response('home.html')

def hello_page(request):
    now = datetime.datetime.now()
    html = "<html> <body bgcolor='lightgrey'><h1><center><font color=red>BTMS</font></center></h1><hr><h3>welcome to my home page and  the current date and time is %s </h3></body</html>" %now
    return HttpResponse(html)




url.py


from django.conf.urls import patterns, include, url
from login.views import hello_page, home
#from django.contrib import admin

#admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'btms.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

   # url(r'^admin/', include(admin.site.urls)),
    url(r'^$', hello_page),
    url(r'^home/', home),
)


o/p (from templates folder)











No comments:

Post a Comment