Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django -- How to have a project wide templatetags shared among all my apps in that project

Second time asking more details ...

I'd like to have a project wide templagetags directory to have the common tags used by all Apps, then each app can have their own tags if need so.

Let say that I have:

proj1/app1 proj1/app1/templatetags/app1_tags.py  proj1/app2 proj1/app2/templatetags/app2_tags.py  proj1/templatetags/proj1_tags.py  proj1/templates/app1/base.html proj1/templates/app1/index.html proj1/templates/app2/base.html proj1/templates/app2/index.html 

Where:

proj1/templates/app1/base.html ----------- {% load proj1_tags %} {% load app1_tags %}  proj1/templates/app1/index.html ----------- {% extends "base.html" %}  proj1/templates/app2/base.html ----------- {% load proj2_tags %} {% load app2_tags %}  proj1/templates/app2/index.html ----------- {% extends "base.html" %} 

Would this work? It didn't work for me. It can't find the proj1_tags to load.

like image 484
un33k Avatar asked May 21 '09 00:05

un33k


People also ask

How do I register a tag library in Django?

Registering the tagThe tag() method takes two arguments: The name of the template tag – a string. If this is left out, the name of the compilation function will be used. The compilation function – a Python function (not the name of the function as a string).

What is custom tags in Django?

Django offers a variety of built-in template tags such as {% if %} or {% block %}. However, Django also allows you to create your own template tags to perform custom actions. The power of custom template tags is that you can process any data and add it to any template regardless of the view executed.

What are template tags in Django?

The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.

What does the built in Django template filter safe do?

In the example above, the is_safe=True argument to the registration decorator tells Django that this filter promises not to output any troublesome characters. A safe string passed to a filter does not get escaped by Django. The default value for is_safe is False .


2 Answers

I don't know if this is the right way to do it, but in my Django apps, I always place common template tags in a lib "app", like so:

proj/     __init__.py     lib/         __init__.py         templatetags/             __init__.py             common_tags.py 

Just make sure to add the lib app to your list of INSTALLED_APPS in settings.py.

like image 176
mipadi Avatar answered Oct 11 '22 01:10

mipadi


Since Django 1.9, it is no longer necessary to create additional common app as others mentioned. All you need to do is to add a path to your project templatetags directory to the settings.py's OPTION['libraries'] dict.

After that, these tags will be accessible throughout your whole project. The templatetags folder can be placed wherever you need and can also have different name.

Customized example from the Django docs:

OPTIONS={     'libraries': {         'myapp_tags': 'path.to.myapp.tags',         'project_tags': 'project.templatetags.common_extras',         'admin.urls': 'django.contrib.admin.templatetags.admin_urls',     }, } 
like image 40
David Viktora Avatar answered Oct 11 '22 01:10

David Viktora