What is the best practice in structuring templates in your Django project? - python

I know there's a lot of questions regarding best practices of how to structure an entire Django project but i cannot find a concrete explanation on how to structure templates for a Django project.
Obviously, I've just started learning Python/Django and i'm quite confused why most of the tutorials/examples i found, the directory name under templates directory is always the same as the applications directory name.
For example:
gazette/
__init__.py
models.py
views.py
static/
...
templates/
gazette/
__base.html
__l_single_col.html
__l_right_sidebar.html
__l_left_sidebar.html
_article_full.html
_article_summary.html
_author_list.html
_author_name.html
_category_list.html
_navigation.html
_tag_list.html
article_detail.html
article_list.html
author_list.html
category_list.html
tag_list.html
this actually came from a blog: https://oncampus.oberlin.edu/webteam/2012/09/architecture-django-templates
In the application structure above, the application gazette/ have templates/ directory under it. In turn, under templates/, there's a single directory called gazette/, which is the same name as the application directory where it's contained. Under this folder are the actual html template files.
Question: Why can't we just directly put all the html template files under templates/ directory?
I would understand the logic of the structure if there are several directories under templates folder. However, i never found a single example from any tutorials in YouTube or blogs that shows a project structure where there are several directories under templates directory. Instead, they have to name their one and only directory (under templates) same as the the application directory.
If you could explain the principle behind it and direct me to any reference/link, i would greatly appreciate it.
Thanks in advance

One alternative for storing templates is to store each app's templates inside the templates directory of the app.
So the project structure would look something like this
Project
app1
templates
app1
template1.html
template2.html
app2
templates
app2
template1.html
This way you can render the template from the view by passing 'app1/templates1.html'

Your Django Project Template at the end of the day is a personal choice, yes there are known templates that optimize your workflow for most projects, but at the end of the day every project is different, for example this is the template i use when i work for someone who expect me to follow the guidelines
Project
app1
app2
..etc
static
app1
images
css
js
app2
...etc
templates
app1
file.html
...etc
app2
..etc
log_files
app1_info.log
app2_info.log
app1_erros.log
..etc
this is because the Django philosophy is based on separation, Django is built around encouraging modularity
when i'm working on personal projects, i like to mix everything because even if use different apps for different things, i'll still have the same style and same templates across most of them.

Related

Where should a Django homepage template and view exist in a large-scale project?

On a large-scale Django project, where should the homepage template and view exist within the project structure?
In its own app (ex: homepage app)?
Some other app (ex: accounts)?
At the project-level putting the template in a "templates" directory and the view somewhere?
Somewhere else?
Is there a most frequent answer to this question?
Definitions:
Large-scale is defined as let's say 15 apps
The homepage content is mostly static now, with plans to get more dynamic as the project evolves
Assumption: project structure strategy is dependent on the size of the project.
The best approach in my opinion implement everything as separate app because of code re-use. You can create app called "landing" with templates folder inside with nested "landing" directory what's how Django might find you templates automatically from GenericViews for example.
yourproject/
landing/
templates/
landing/
index.html
urls.py
models.py
views.py

How do I define custom filters for minimal use of django templates?

I have a python project that I'm using Django templates for to generate C++ source code.
I picked Django because the template language is quite restrictive and has a very large community making it easy for end-use developers to use and get help with.
I'm failing to add custom filters for my project (to translate one set of type names into another) because I have not done the normal django setup.
Instead:
from django.template import Context, Template
import django
if not django.conf.settings.configured : django.conf.settings.configure()
django.setup()
Lets me use Django templates perfectly but not define custom filters.
My custom filter is called ctypes_filters.py and I reference it in the template as
{% load ctypes_filters %}
Running my generation script results in the following error:
django.template.base.TemplateSyntaxError: 'ctypes_filters' is not a valid tag library: Template library ctypes_filters not found, tried
django.templatetags.ctypes_filters
How can I get django to find the filter without setting up a full Django project (database definitions etc)?
I know that other templating solutions are available (and are probably more light-weight) but I'm really keen to use Django's simple and elegant templates.
The location of Django template tags is done by convention rather than a configuration setting (see the code layout section of the template tags docs).
Put the ctypes_filter.py in a templatetags directory in an installed app (I've called it myapp here). Add an empty __init__.py to both the myapp and templatetags directories. The app doesn't need any other files you might commonly find in a Django app, like models.py or views.py.
myapp/
__init__.py
templatetags/
__init__.py
ctypes_filter.py
Then include myapp in your INSTALLED_APPS when configuring your settings.
django.conf.settings.configure(
INSTALLED_APPS=('myapp',),
)

Flask-Admin (python) - Having trouble overriding templates

I am attempting to utilize Flask-Admin for an administrative interface to my web service.
I have it working, but the theme does not match what the rest of my site uses. The documentation here suggests that it is as simple as overriding the master template, but when I do that I end up with circular reference errors.
I have also tried on individual templates by copying the templates from the install directory to my application structure, but I cannot figure out the path they use. It is like it just defaults to the install directory, even if I have templates of the same name local to my flask app. From the docs: "You can override any used template in your Flask application by creating template with same name and relative path in your main templates directory."... yet I am not able to do that. Does it still expect admin/ in front of the templates?
Does anyone have an example? I basically need to override the bootstrap theme used, but some other customization could be nice. I'm new to flask, and python for that matter, so this may be quite simple...
You will still need to place your templates in the admin sub-folder of templates:
yourapp/
app.py
templates/
master.html # <-- This will not override admin/master
admin/
master.html # <-- This one, however, will :-)

Custom django admin templates not working

I've been trying to get custom templates for the admin page for Django working but have been unsuccessful. I've read the django documentation and several blogs which explain it as being such an easy step, which I assumed it was.
As of right now the admin page works but my own rewrite of the CSS or templates is not working. My setup is as follows
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/app/
views.py
models.py
__init__.py
/templates/
/admin/
base_site.html
In the urls.py I have
(r'^admin/', include(admin.site.urls)),
Which works since I cannot login etc. So I am assuming the /admin/base_site.html would overwrite the default one but it isn't doing a thing.
Anyone know what is going on here ? I followed it from the Django tutorials/guides and went onto some blogs to see if they had answers but they all said the same thing.
Edit 1:
I do have my templates directory setup correctly.
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates/'),
)
This works correctly as I have the rest of my site working with a media directory for CSS etc. The only thing not seeming to 'accept' the templates is the admin section.
Alright I fixed it, this was a stupid mistake but I was already playing with this for the past 2 hours. I had to declare my app before django.contrib.admin. It wouldn't accept it otherwise.
One more mistake that one should resist making on this exercise. The exercise says to change this...
<h1 id="site-name"> {{ site_header|default:_('Django administration') }} </h1>
to this...
<h1 id="site-name">Polls Administration</h1>
There is a temptation to only change the string constant, but that is incorrect. I.e. do NOT do this, it will not alter the heading:
<h1 id="site-name"> {{ site_header|default:_('Polls Administration') }} </h1>
That was the mistake I made, and I had to go through the exercise meticulously to fix it.
I couldn't get the admin template to be recognized when doing this step in part two of the Django tutorial.
This is how I solved it:
Using the information in this answer, I printed the value of TEMPLATE_DIRS:
At the command line, navigate to the directory in which the project's settings.py file exists (for me this is R:\jeffy\programming\sandbox\python\django\tutorial\mysite\mysite\)
Start the interactive shell: python
>>> import settings
>>> settings.TEMPLATE_DIRS, which outputs ['R:\\jeffy\\programming\\sandbox\\python\\django\\tutorial\\mysite\\templates']
So this is the expected location of the templates directory for this project. The admin directory goes into that, and the base_site.html file goes into that.
The other problem I had was that it was working, but I only changed the <TITLE> field, so I just didn't notice it. (I thought I was changing the main header.)
I had this same problem walking through the Django 1.6.5 tutorial (https://docs.djangoproject.com/en/1.8/intro/tutorial02/), but realized it was a mistake in not reading carefully. The tutorial has you do is put this into your settings.py:
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
}
I then put the templates folder I wanted to put override for the admin site under the app just like your example:
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/app/
views.py
models.py
__init__.py
/templates/
/admin/
base_site.html
With this it wouldn't work for the reason similar to the issue you had noted by Daniel Roseman in one of the comments, my DIR was evaluating to project_folder/templates (as I told it to). Then I noticed in the tutorial it explicitly said put the templates/admin folder at the project level, not the app level:
Create a templates directory in your project directory (the one that contains manage.py). Templates can live anywhere on your filesystem that Django can access. (Django runs as whatever user your server runs.) However, keeping your templates within the project is a good convention to follow.
By doing this I had the following structure:
/project_folder/
manage.py
settings.py
urls.py
__init__.py
/templates/
/admin/
base_site.html
/app/
views.py
models.py
__init__.py
With this, the everything worked as expected (I could overwrite the default django templates for the admin pages).
While you should be able to put templates anywhere and configure Django to find them, it seems to makes sense to put your main admin templates at the project level, as the admin site's not app specific, but available for the entire project.
Make sure you have set TEMPLATE_DIRS in settings.py to the correct folder!

Python and App Engine project structure

I am relatively new to python and app engine, and I just finished my first project.
It consists of several *.py files (usually py file for every page on the site) and respectively temple files for each py file.
In addition, I have one big PY file that has many functions that are common to a lot of pages, in I also declared the classes of db.Model (that is the datastore kinds).
My question is what is the convention (if there is one) of arranging these files.
If I create a model.py with the datastore classes, should it be in different package?
Where should I put my template files and all of the py files that handle every page (should they be in the same directory as the one big common PY file)?
I have tried to look for MVC and such implementations online but there are very few.
Thanks,
Joel
I usually organize my projects in this way:
project
main.py
README
models
bar.py
foo.py
views
foolist.hml
barlist.hml
controllers
controller1.py
controller2.py
api
controllerapi.py
helpers
utilities.py
lib
extfoo.py
db
foo.db
test
test.py
Look at this post; it's a really great article on how to structure a project (not in python but it does not matter).
Typically I organize like so:
project/
main.py
models.py
app.yaml
index.yaml
templates/
main.html
foo.html
...
styles/
project.css
js/
jquery.js
project.js
images/
icon.png
something.jpg
And I have all of my handlers in main.py, all of my models in models.py, etc.
If I have a lot of handlers, and I can easily split the functionality of some handlers from the others (like taskqueue handlers vs. request handlers vs. xmpp/email handlers) I'll add another foo_handlers.py to the mix, but usually I just cram them all in main.py
But then again, I tend not to write hugely complex Python App Engine apps...

Categories