unable to change default django template - django

I was going through the tutorial of Django - https://docs.djangoproject.com/en/1.3/intro/tutorial02/
At the end they asked us to change the name of default webpage template from "Django Administration" to anything else by following steps.
1) Copy base_site.html from default folder of django to the admin directory specified in TEMPLATE_DIRS.
Currently my TEMPLATE_DIRS looks like -
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
"/home/mysite/polls/mytemplates"
)
I have also copied the admin template directory to /home/mysite/polls/mytemplates
So the structure is something like this
/home/mysite/polls/mytemplates/admin
where admin folder contains base.html and base_site.html
I have edited the title in base_site.html file but still when i run django it shows me default template title.
Can someone tell me what am i missing?

Your template directory should not have your application name in it. Django will automatically add any directory called templates that is part of any app listed in INSTALLED_APPS. This is why your custom admin template isn't working.
The first thing you should do is rename /home/mysite/polls/mytemplates to templates:
mv /home/mysite/polls/mytemplates /home/mysite/polls/templates
Next, you should have:
TEMPLATE_DIRS = ('/home/mysite/templates',)
Then, your admin templates should be in /home/mysite/templates/admin/
As for the comma : a single string in parenthesis is just a string, but with a comma it becomes a tuple. The TEMPLATE_DIRS setting is a tuple, which is why you need that extra comma:
>>> a = ('hello')
>>> type(a)
<type 'str'>
>>> a = ('hello',)
>>> type(a)
<type 'tuple'>

I figured it out. The issue was with the single quote that i had in my site name. So had to enclose the entire site name in Double quotes instead of one

Related

Django Templates: How does it decide where to load a template?

I was following the Django Girls Tutorial up to the point where we add the login capabilities and I've gotten to a point where it tries to load from a different template folder than I want.
I have all my blog templates in blog\templates\blog\, etc. and that is where Django is looking for my login.html. However, in the tutorial (and as a result in my folder structures), the path for login.html is: mysite/templates/registration/login.html. So how would I make Django look there?
Sorry if this is a stupid question.
Django will look to your settings.py for how to load templates, and the order in which it should try to load templates. It's likely that you haven't configured django to look for templates in mysite/templates.
There's a setting called TEMPLATE_DIRS which is a list of absolute paths for your template folders. So in your settings.py file, try something like below. Read my comments to see what each line does.
# create a variable that stores the absolute path to your project's root directory
# you might have something like this already defined at the top of your settings.py
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
# this is the default value for TEMPLATE_LOADERS
# which says to look at the `TEMPLATE_DIRS` variable, and then in each of your app's subdirectories for templates
TEMPLATE_LOADERS = ('django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader')
# the special sauce. tell django to look at the "templates" folder
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates'), )

Custom base_site.html not working in Django

I am using Nitrous for playing with the Django framework. In tutorial 2 is shown how to change the base_site.html template. I've added in the TEMPLATE_DIRS = ( ) a new line:
'home/action/workspace/mysite/templates',
And in base_site.html I changed the site name title of Django Administration into Administration:
{% trans 'Administration' %}
But I still see no changes on the website. I've tried different TEMPLATE_DIRS like:
'~/workspace/mysite/templates',
'home/action/workspace/mysite/',
'home/action/workspace/mysite/templates/',
And restarting the server. But I am doing something wrong.
Note that this answer was written before the TEMPLATES setting was introduced in Django 1.8.
The tutorial says you should create an admin subdirectory inside your template directory, and put your template inside that.
Try with base_site.html in /home/action/workspace/mysite/templates/admin and with
TEMPLATE_DIRS = (
'/home/action/workspace/mysite/templates',
)
Note the slash before /home/... and the trailing comma to make it a tuple.

Django-Oscar override templatetags

I tried to override category_tags by creating a local version of catalogue application with a local templatetags folder which has a category_tags.py file (also init.py).
It seems the default category_tags file is used.
If I change the name of my local tag file and of course I load the new name of the tag in the template it works but I would like to override or extend the default category_tags not to create another one.
Any ideas?
thanks!
I got it working by putting the templates in my project_folder > templates > oscar_app_name > template_name.html
So to override the "checkout" app and template, my structure looks like this:
/project
--/apps
----/checkout
------[change checkout models]
----__init__.py
----app.py
--/project
----/templates
------/checkout
--------payment_details.html
----__init__.py
----settings.py
----urls.py
--manage.py
And then you have to edit settings.py Installed Apps
INSTALLED_APPS = [
'django.contrib.admin',
...
'compressor',
'paypal',
] + get_core_apps(['apps.checkout'])
I would assume that template tags get loaded in the order specified in INSTALLED_APPS. Does the app containing your category_tags.py come before (or instead of, e.g. when using Oscar's get_core_apps override to extend an Oscar core app) oscar.core.apps.catalogue?
PS. I'm an Oscar author and have to admit we don't check StackOverflow very often. The mailing list is your best bet at the moment.

Tell django to search app's template subfolders

I have the following folder structure for the templates on my django app:
templates/
app/
model1/
model1_form.html
model2/
model2_form.html
Suppose I'm using model1 and a generic ListView, right now it only searches at templates/app/model1_form.html. Is there anyway I can tell django he should also search the app/ subfolders? I don't want to have to set the template name and path manually (template_name="templates/app/model1/model1_form.html").
At settings.py I have:
import os.path
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
BASE_PATH+'/templates/',
)
This is my view:
class HousesListView(ListView):
model = House
context_object_name = "house_list"
Thanks in advance!
The other answers were correct at the time, but for anyone coming across this now, this is how this is done in Django 1.8+
By default, Django now looks in app folders for templates. This is indicated by 'APP_DIRS': True, in the TEMPLATES setting like this:
TEMPLATES = [
{
...
'DIRS': ['templates'],
'APP_DIRS': True,
...
},
]
As the other answers indicate, you should still use appname/templates/appname/model1_form.html to namespace the templates
( more info: https://docs.djangoproject.com/en/1.8/intro/tutorial03/#write-views-that-actually-do-something in the box titled 'template namespacing')
Final check: make sure the app you're using is in the INSTALLED_APPS tuple, as that was the problem I was having.
You need to add django.template.loaders.app_directories.Loader to TEMPLATE_LOADERS (if it's not already).
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
Then, change your folder structure such that you have a "templates" folder in the app directory:
- <project_root>
- app
- templates
- model1
- model2
Or to properly namespace the models so they don't clash with other app names accidentally:
- <project_root>
- app
- templates
- app
- model1
- model2
I think to use templates from subfolders generally you either have to:
Manually give the template path as subdir/template.html
eg. render_to_response('sub/dir/template.html') or template_name='...'
Define all the subfolders in the TEMPLATE_DIRS
eg.
TEMPLATE_DIRS = (
BASE_PATH+'/templates/',
BASE_PATH+'/templates/app',
BASE_PATH+'/templates/app/model1',
)
But because Generic Views except templates to be found in app_name/template.html,
you would have to move your templates to add /path/to/templates/app/model1 to your TEMPLATE_DIRS, and move your templates to templates/your_app/model/your_app/model_template.html, which is little bit awkward.
With Django 2.0, it's not even necessary to change anything in the TEMPLATES variable in settings.py.
The only requirement is to add appname.apps.AppnameConfig (e.g. catalog.apps.CatalogConfig for an app named 'catalog') to the INSTALLED_APPS list, then django will look under appname/templates when it searches for templates.

Django Admin: using different templates for two admin site

I've a Django project with two different admin-site (as described in documentation )
I would like to have different custom template for each of them.
I know how to override custom template, by putting html files in myproject/templates/admin/ directory.
However, both admin-site use those templates !
I don't understand how to specify another set of custom templates.
Ideally, I would like having:
# For first admin site
myproject/templates/admin-a/
base.html
base_site.html
and:
# For second admin site
myproject/templates/admin-b/
base.html
base_site.html
first option will be to have two ModelAdmin classes, one derived from second one, with some additional parameters defining templates, here is part of the admin code:
# Custom templates (designed to be over-ridden in subclasses)
add_form_template = None
change_form_template = None
change_list_template = None
delete_confirmation_template = None
delete_selected_confirmation_template = None
object_history_template = None
above variables can be set in your admin class.
second way is to pass a base template name into the template, and then use this (variable) as a parameter to the extends template tag. Documentation here.
third option wil be to have a two instances of code running, but with two configs with different setting variable TEMPLATE_DIRS, first one e.g.:
TEMPLATE_DIRS = ('templates-a',)
second
TEMPLATE_DIRS = ('template-b', 'template-a')
Having both template dirs here gives you an fallback option, so you will define only those templates which are different.
Third option is easiest to implement (no change to the code) but it requires 2 separated instances working at the same time (more system resources consumed).