Thursday, April 21, 2016

Base Template in reusable Django App

Leave a Comment

I want create a reusable app from a custom project.

The part I want to move to a reusable app serves whole pages and uses this at the top:

{% extends "myproject/base.html" %}

Now I am unsure what base template to use. In the new reusable app, I don't want to use myproject since this is custom code which I can't share.

I tried to get help from the great django docs, but could not find a solution - Forgive me if I was blind :-)

What to write here in a reusable app?

{% extends ??????? %}

3 Answers

Answers 1

You need to store those templates within the app folder.

Assuming you have a structure like myproject/myreusableapp you would create a templates folder inside myreusableapp folder and store your base.html file there.

You would then extend that file {% extends "myreusableapp/base.html" %}

Answers 2

Unfortunately django does not offer an official convention here.

There is a third party documentation project which defines these rules for reusable templates:

https://django-reusable-app-docs.readthedocs.org/en/latest/apps/templates.html

The issue to get a convention in the official django docs is open since 2012:

https://code.djangoproject.com/ticket/19106

Here is my ticket, which was closed as duplicate of above:

https://code.djangoproject.com/ticket/26501

... I hope an agreement will be found in the future.

Answers 3

I do it this way :

\project | +-\main | | | +-\template | | | | | +-\mainapp | |   | | |   +-base.html | |   +-index.html | |   +-... | | | +-\static |   | |   +-css |   +-js |   +-pics |   +-... | +-\otherapp   |   +-... 

Where main is an app for standard operations (pager, login,...) and has the base templates and static for in it. Note that templates are stored in main/templates/main/.

In project settings.py, don't forget to register main/static in the STATICFILES_DIRS.

Then you can deploy using {% extends 'main/base.html' %}.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment