Where to put templates in Django 1.4? - python

I got a bit confused, because when I use the generic ListView class, Django is looking for "appname/objectname_list.html" inside the "appname/templates" folder. This results in:
appname/templates/appname/objectname_list.html
If I supply "template_name", it does not require the subfolder inside the templates folder anymore.
Why doesn't it find the objectname_list.html inside the "templates" folder like before in 1.3 ? Did I configure something wrong or did they change the place where I'm supposed to put my templates as well ? In 1.3 I used to place template html files inside "appname/templates" directly instead of using a subfolder.
For me it does not make sense to use another subfolder with the name of the app, where the templates folder is already in. Or am I missing something useful ?
I could not find anything about it in the release notes.

I don't think this is 1.4 specific. If you will want your template in a subfolder, specify template_name = 'subfolde/template.html'
Regarding template dirs, from the docs:
... For each app in
INSTALLED_APPS, the loader looks for a templates subdirectory. If the
directory exists, Django looks for templates in there.
So the actual dir is:
projectname/appname/templates/appname/objectname_list.html
^^^ ^^^
This helps you keep everything inside the app folder and does not require you to configure anything,
This also means that if you have apps foo, bar and baz, you can still have one templates dir under one of the apps:
myproj/foo/templates/foo/...
myproj/foo/templates/bar/...
myproj/foo/templates/baz/...

Related

Changing the name of the sub folder inside Django Template path

I am new to Django and I have a path to my HTML file as
./templates/registration/login.html and I want to change it to
./templates/index/login.html
After renaming it to /templates/index/login.html, it is still picking up the old directory /templates/registration, could not find login.html screen and it throws an error.
Could someone tell me what changes we have to make in settings.py when we rename a folder name inside templates structure?
In your settings.py, you can define the templates location, which applies the template function per app.
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates')
)
I assume your app is named registration, hence why it is looking there. You can either rename your app or place all your templates in a global folder at the same level as the apps (and not within the apps themselves). The latter is not recommended for large applications.
Edit: Why is this labeled Python2? What version of Django is this?

Django wrong precedence of templates?

It took me forever to find out why a template was not getting overridden, only to find that it seems Django simply does not use the correct precedence in overriding the templates.
The templates I'm trying to change are the ones for changing the user's password, which are loaded for the URL /accounts/password/change.
I have my modified templates in mysite/myapp/templates/registration; the system default templates are at /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/templates/registration. When templates are present in both these directories, Django uses the system (/Library) ones. Huh?
When I remove the templates in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/templates/registration
, the correct ones (i.e. mine, in mysite/myapp/templates/registration) are loaded.
How to fix this issue? That is, how to make Django load my overridden templates without having the delete the original templates found in the /Library?
you have to create an admin directory inside the templates dir in which you set the templates you want to override
In order to override one or more of them, first create an admin directory in your project’s templates directory. This can be any of the directories you specified in TEMPLATE_DIRS.
Read the Django doc here

How to handle assets (css, js, etc) with Django?

I'm new to Django and to Python, and I've just completed Django's "build your first app" tutorial. I found it fantastic. However it never spoke about stylesheets, javascripts or anything of the like.
Where should they go? Currently my project directory looks like this:
myproject
+app1
+app2
+myproject
+public
+css
+img
+js
+templates
Am I doing it correctly? I would like to stick to Django/Python standards as much as possible. Also, if I would like to include certain styles and scripts in all of my templates, how can I do that? Let's say I'm using Bootstrap.
You should probably take a closer look at Managing static files.
Basically you can put a static folder in each of your apps (similar to your template folders) and the more general stuff (base.css, project-wide icons, jquery-plugins) in a static folder in your project root.
So the layout might look like this:
myproject
+app1
+static
+app1
+css
+js
+app2
+static
+css
+img
Once you deploy your project you can use, the collectstatic-command from contrib.staticfiles to collect all your static files to a single directory (STATIC_ROOT) where you can serve it from.
I prefer to call the +public folder +static, and put the +template (app specific) folder inside the application that is using them (so to avoid confusion and to be able to use a template with a different name twice), also I don't understand why you put +myproject inside your root project.

Django : How to structure template folder considering template tag includes?

I have my template folder with all html templates lieing together in the template folder with no directory structure as such.
I decided to arrange them on per app basis, but:
A template with template-tags belong to different apps.
Eg:
Login page(Login app) includes a banner that belongs to UserActivity [User activity app]. So, if I include the login template in login folder in templates, then it will be including stuff across other app's template folder.
How should I structure so that all that referred stays in 1 place organized ?
Feel free to ask for more info.. :)
Organizing your templates in subdirectories is definitely they way to go, but I am not sure if you can really reach the level of separation you are looking for.
If your apps depend on each other you'll always have includes and tags from other apps. So i'd put the templates to the app they belong to.
But maybe the docs about template loaders can help you clarify your structure.
For example the app_directories.Loader
Loads templates from Django apps on the filesystem. For each app in
INSTALLED_APPS, the loader looks for a templates subdirectory. If the
directory exists, Django looks for templates in there.
This means you can store templates with your individual apps. This
also makes it easy to distribute Django apps with default templates.
So you could put app-specific templates in in your app directories and keep your general templates (base.html, etc.) in the top level template dir of your project.

Why absolute paths to templates and css in django ? (Isn't that a bad practice ?)

In django, the documentation asks to use the absolute paths and not the relative paths.
Then, how do they manage portability ?
If I have my template in the project folder then, even a rename of the folder will cause breakage.. !
Then what is the reason behind this practice ?
Please explain ?
Could you post a link to that piece of documentation, please?
In Django you configure, in settings.py, the search path for templates (through TEMPLATE_DIRS variable). Then, inside a view, you render a template naming its file relative to one of the path included in TEMPLATE_DIRS. That way, whenever you move you template dir you just need to modify your settings.py
As for static files, like CSS docs, Django does not need to know anything about them (unless you are serving static files through django itself, which is discouraged by django's documentation): you only need to tell your web server where to find them.
I switch environments from a Linux desktop to a Windows laptop, so hard coding paths won't work for me either.
There may be a better way to do this, but I wrote this function that goes at the top of my settings.py to get the absolute path from a relative path in my project:
#settings.py
import os
def map_path(directory_name):
return os.path.join(os.path.dirname(__file__), \
directory_name).replace('\\', '/')
That allows me to do:
MEDIA_ROOT = map_path('static')
TEMPLATE_DIRS = (
map_path('templates'),
)
"static" and "templates" live under my project root. Hope that helps you out.

Categories

Resources