While setting up my project and working to keep apps non-dependent, I've hit a snag. I'd like all of the templates from the different apps to have a consistent header and footer. Here's what I'm trying:
myproject/
base/
templates/
header.html
footer.html
app1/
templates/
my_app1_page.html -> want to include 'header.html'
and 'footer.html' from base app
Pretend there are many more apps that want to do this as well. Is this possible and/or the right way to do it?
As long as the apps are in INSTALLED_APPS and the template loader for apps dirs is enabled, you can include any template from another app, i.e.:
{% include "header.html" %}
... since your templates are located directly in the templates dir of your app.
Generally, in order to avoid name clashes it is better to use:
app1/
templates/
app1/
page1.html
page2.html
app2/
templates/
app2/
page1.html
page2.html
And {% include "app1/page1.html" %} or {% include "app2/page1.html" %} ...
But: for keeping a consistent look and feel, it is so much better to use template inheritance rather than inclusion. Template inheritance is one of the really good things of the Django template system, choose inheritance over inclusion whenever it makes sense (most of the time).
My recommendations:
Have a base template for your project ("base.html" is the default convention) with header and footer and a {%block content%} for your main content.
Have your other templates inherit form base.html {% extends "base.html" %} and override the content section
See another response to this question for links to the doc
While you can certainly do that by using the include tag and specifying absolute paths, the proper way to work in Django is by using Template inheritance.
If you start a project with "$ django-admin startproject project" a folder named "project-folder-name" i.e. project/ is created. After adding a few apps and adding the apps in the "settings.py" -> INSTALLED_APPS=[..., app1, app2] and creating a templates folder within the project/ I got structure like this:
project/
project/
templates/
base.html
app1/
templates/
app1/
page1.html
page2.html
app2/
templates/
app2/
page1.html
page2.html
in template app1/template/page1.html I wrote
{% extends 'base.html' %}
and the "TemplateDoesNotExist at /" Error message appeared.
Then I added another App named "core" and added base.html to the template folder (+edit INSTALLED_APPS in settings.py) and i got this structure now:
project/
project/
templates/ (unused)
base.html (not seen)
core/
templates/
base.html
app1/
templates/
app1/
page1.html
page2.html
[...]
Now the error message disappears and the base.html is found with templates/app1/page1.html:
{% extends 'base.html' %}
You can change the folder structure like this:
core/
templates/
core/
base.html
then you need to change the template app1/page1.html to
{% extends 'core/base.html' %}
as well.
As an alternative you can also add "your-project-name" in this explaination "project" into your settings file like this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'project', # your project name main folder
'core',
'App1',
'App2',
]
And now django finds the project/templates/base.html as well. However I don't know if this solution is recommended.
project/
project/
templates/
base.html (now it is found)
P.S. Thanks (my upvotes aren't counted yet) and comment me if this answer was somehow clear and understandable
Related
This is my directory structure
app_web
_init_.py
settings.py
urls.py
wsgi.py
upload_app
migrations/
static/
js/
alert.js
templates/
upload.html
_init_.py
admin.py
apps.py
models.py
tests.py
views.py
db.sqlite3
manage.py
In settings.py , my
STATIC_URL = '/static/'
and in my upload.html
{% load staticfiles %}
<script type="text/javascript" src="{% static "js/alert.js" %}"></script>
It does not works and throws 404 error everytime. I even tried load static but it still cannot load anything from static folder and throws 404 error.
I am using Windows 10 machine and Django==1.9
The idea was to create a static directory outside the upload_app folder. The reason is that in the settings.py the BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) . That means one directory above is the base directory, so it will start searching static/ from that directory. But it will never find it as I placed inside upload_app/static .
Also, I need to work on putting templates outside the app upload_app as it's not generally best practice to keep templates inside the app.
looking for other suggestions in structure
I have followed several django tutorials on the web.
There is 2 folders: One for django project and another for application. We can have multiple applications for the same project but i my case, i have just one.
Sometimes, the application folder is located at the same level than project folder and sometimes, the application folder is located inside project folder.
I do not know what is the best solution... It works in both case. It is just a state of the art question
Thanks
Mostly it is a matter of choice and the organization of your Project. Even thought, i will post you here a recomented layout and good reasons to choose this
myproject/
manage.py
myproject/
__init__.py
urls.py
wsgi.py
settings/
__init__.py
base.py
dev.py
prod.py
blog/
__init__.py
models.py
managers.py
views.py
urls.py
templates/
blog/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_managers.py
test_views.py
users/
__init__.py
models.py
views.py
urls.py
templates/
users/
base.html
list.html
detail.html
static/
…
tests/
__init__.py
test_models.py
test_views.py
static/
css/
…
js/
…
templates/
base.html
index.html
requirements/
base.txt
dev.txt
test.txt
prod.txt
Allows you to pick up, repackage, and reuse individual Django applications for use in other projects
Environment specific settings. This allows to easily see which settings are shared and what is overridden on a per environment basis.
Environment specific PIP requirements
Environment specific PIP requirements
Small more specific test files which are easier to read and understand.
I have two apps. Both have different base.html templates that index.html extends from. The index.html is different though on each app.
But for some reason it only extends from the same app with base.html? How is this possible?
This is how it looks in both of my my index.html templates:
{% extends 'base.html' %}
How is your template/ directory structure set up? If an index.html template extends from base.html, Django will choose whatever base.html is in your root template directory.
Solution:
Either rename one of your base.html templates to something like base2.html and put it in templates/ alongside base1.html, or create new directories in templates/ to put the base.html files into.
For solution A, make sure you change {% extends base.html %} to {% extends base2.html %} in the appropriate index.html template.
For solution B, your base.html files would keep the same name, but be in different directories. So one is in say templates/base1/base.html and the other is in templates/base2/base.html. Your index.html files would extend like {% extends base1/base.html %} and {% extends base2/base.html %}. Note that all extension paths are relative to the root of your chosen template directory.
IMO solution B is better as it separates the code for each template base into different, explicitly named folders. Better organization/flexibility and less confusion for you in the future.
I am following the tutorial in the offical documentation https://docs.djangoproject.com/en/1.7/intro/tutorial02/
I am in the section of customizing the templates, but I am running into problems with changing the content of the templates
This is in my settings.py
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
print TEMPLATE_DIRS
Here is my tree structure
└── mysite
├── manage.py
├── mysite
├── polls
└── templates
└── admin
└── base_site.html
I double check the path using print, which should be correct
['/Users/mysite/templates']
and edit the base_site.html to
{% block title %}{{ title }} | {{ site_title|default:_('Poll admin') }}{% endblock %}
However it seems like I cannot see the changes when I rerun
python manage.py runserver
The other questions seems to have a path problem, but I think my path is correct,
http://stackoverflow.com/questions/4921080/how-do-i-change-templates-on-django-admin-pages
I have two apps, consisting of adminApp and mobileApp. Both have their own template directory and totally seperated. Unfortunately django keeps looking in the wrong directory when i eg. use extend, it looks into the first template directory according to the order in INSTALLED_APPS:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
INSTALLED_APPS = (
...
'adminApp',
'mobileApp',
...
)
My directory structure is:
adminApp/
templates/
base.html
mobileApp/
templates/
base.html
So basically what happens is that when i have a file such as mobileApp/templates/pages/index.html with a {% extends "base.html" %} it uses adminApp/templates/base.html instead of its own.
How can i avoid this and keep them separated?
Your apps don't follow the Django conventions for template directories. The app name should be repeated to avoid this issue:
adminApp/
templates/
adminApp/
base.html
mobileApp/
templates/
mobileApp/
base.html
You can then unambiguously specify which template you wish to extend:
{% extends "mobileApp/base.html" %}