Justin
Justin

Reputation: 885

Multiple Templates, Complex View or Creative Model

I am new to Django and am having a tough time deciding on what is the best approach to design my templates. The way I have developed my site thus far has been relatively simply. I am now at a point where I have a homepage which three different anchor tags pointing to the same template, but will ultimately be generated dynamically based on which link was selected.

My question is which is the best solution:

Should I build three separate (lets call them path) templates each for the unique url that it represents?

Should I build one single template and then beef up my view and pass in a variable and generate it that way off of one template returning whatever data is necessary based on the input.

Or, is there a solution making use of a model for each page?

DRY seems to lend itself toward a single template with complexity in the view, but my instinct seems to want to think that multiple templates would be easier to maintain.

Thanks for your input.

Upvotes: 0

Views: 204

Answers (2)

krs
krs

Reputation: 4154

With django you use the {% extends %} template tag to inherit another template, then you overide the {% block %} you need to be specific.

ie.

base.html

<html>
    <head>
       <js and css etc>
    </head>
    <body>
    <some more html />
    {% block content %}
        <p>generic content that wont be shown if you render specific.html</p>
    {% endblock %}
    </body>
</html>

then in specific.html

{% extends 'base.html' %}

{% block content %}
    <p>specific content that will be shown instead</p>
{% endblock %}

blocks can be small and doesnt have to be overridden, usages for example

<body class="{% block bodyclass %}{% endblock %}">

is common

Upvotes: 1

McAbra
McAbra

Reputation: 2524

You can make a site_base.html with all the <head> and <script> etc. elements and with a

<!-- HEAD -->
  {% block content %}
    <div class='container' id="default_container">
      Your default content
    </div>
  {% endblock content %}
<!-- FOOT -->

Then in your urls.py you determine 'paths' - urls to your views. If you are desireing a simple flat page you can put a TemplateView.as_view(template_name = "page_1.html") like:

urlpatterns = patterns('',
    (r'^$', TemplateView.as_view(template_name='site_base.html')),
    (r'^page_1/$', TemplateView.as_view(template_name='page_1.html')),
)

and in your page_1.html:

{% extends 'site_base.html' %}

{% block content %}
  <div class='container' id="page_1">
    Your Page 1 content that will overwrite `block content` tag from site_base.html
  </div>
{% endblock content %}

You can place any {% block }% tags you want.
I sometimes use my Flat app. It searches for a template with a name from url or returns a 404.

# urls.py
from views import flat_render_view

urlpatterns = patterns('',
    url(r'^(?P<template_name>\w+)/$', flat_render_view, name='flat_view'),
)

# views.py
from django.http.response import Http404
from django.template.base import TemplateDoesNotExist
from django.shortcuts import render

def flat_render_view(request, **kwargs):
    try:
        return render(request, "flat_pages/%s.html" % kwargs['template_name'])
    except TemplateDoesNotExist:
        raise Http404

Upvotes: 1

Related Questions