0

I am quite a beginner in django and I need some advices.

I am trying as much as possible to create reusable django applications that will be used in several different projects. But I don't know how to proceed with templates.

If I have an application managing user, I think the template allowing to add, remove or list a user shall be located in the application and not in the project. Templates project should define headers, footers and general organisation (correct me if I'm wrong).

However, if I want to use template inheritance I will extend project template in my application template :

{% extends "base.html" %} {% block content %} ... {% endblock %}

So in developping my reusable application I make the assumption that my project will have a template called base.html with a block content, and in my mind this information should not be located at application level, but in project level. In some projects I will want to display users in block content, but not necessarily in others. I could want to display user information in several places in the same page for example...

How do you developp your application template to bypass this limitation ?

Thanks in advance,

Bill

billdangerous
  • 91
  • 1
  • 1
  • 6
  • This http://stackoverflow.com/questions/16498176/is-dividing-a-template-into-parts-and-including-each-part-bad/ might help you – Aamir Rind Jun 17 '13 at 12:59

3 Answers3

0

What you are describing is probably best solved with custom template tags, specifically inclusion tags.

Ngenator
  • 10,909
  • 4
  • 41
  • 46
0

I would do a basic html template containing a header and a footer, and many reusable templates extending the basic one, containing the different layouts I would need. I would also create reusable components (tiles, datagrids...).

For the templates :

base.html

<!doctype HTML>
<html>
<head>
....
</head>
<body>
{% block content %}
</body>
</html>

3_columns.html

{% extends "project/base.html" %}
{% block content %}

<div class="line">
    <div class="column">{% block col1 %}</div>
    <div class="column">{% block col2 %}</div>
    <div class="column">{% block col3 %}</div>
</div>

{% endblock %}

2_lines.html

{% extends "project/base.html" %}
{% block content %}

<div class="line">{% block line1 %}</div>
<div class="line">{% block line2 %}</div>

{% endblock %}

A basic custom component :

templatetags/custom.py

import django
from django.template.defaulttags import register

@register.inclusion_tag('components/custom.html')
def custom(params):
    context = {
        'a': params['a'],
        'b': params['b']
    }
    return context

templates/components/custom.html

<div class="custom">
<label>{{ a }}
    <input name={{ b }}
</label>
</div>
Arthur
  • 345
  • 4
  • 10
-1

django-admin.py collectstatic

Read docs

Files are searched by using the enabled finders. The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

Piotr Kowalczuk
  • 400
  • 5
  • 19