python - Make template pluggable in Django -
let i'm writting app "profile" ; have following template (in profile/presentation.html):
<h1>{{user.first_name}}<h1> <p> user likes {{user.hobby}}</p>
which attached view :
# profile/views.py class detailuserview(generic.detailview): model = user template_name = 'members/profile_detail.html' # profile/urls.py urlpatterns = ( url(r'^(?p<pk>[0-9]+)$', views.detailpersonview.as_view(), name='profile')) # project/urls.py urlpatterns = ( .., url(r'^profile/', include(profile.urls), ...)
now "plug" template in project layout:
<!-- project/templates/project/layout.html--> <html> <head> <title>my site</title> </head> <body> <header>the title !!</header> {%block body_block%}{%endblock%} </body> </html>
if want keep uncoupled app, can't inherit "presentation.hmtl" "layout.html".
how can django: when ask /profile/2, take result of view "detailuserview" , plug in layout @ "body_block"?
you're looking template inheritance. docs have lot of information.
project/templates/project/layout.html
<html> <head> <title>my site</title> </head> <body> <header>the title !!</header> {%block body_block%}{%endblock%} </body> </html>
project/templates/profile/presentation.html
{% extends "project/layout.html" %} {% block content %} <h1>{{user.first_name}}<h1> <p> user likes {{user.hobby}}</p> {% endblock %}
Comments
Post a Comment