I'm trying to override the template located in the forms/templates/django/forms/widgets/input_option.html directory, but it's not working for me. Is it possible to do this at all?
You need to create the same template path django/forms/widgets/input_option.html in any of your INSTALLED_APPS.
Django will iterate over all INSTALLED_APPS and look for a template with the given path, django/forms/widgets/input_option.html in your case. The last found file will be used as the template for rendering.
You can find an example and explanation in the django documentation: https://docs.djangoproject.com/en/4.0/topics/templates/#django.template.backends.base.Template.render
Related
I've been writing my first Django app and hit a problem with using templates.
No matter how I format the url for the template I receive the TemplateDoesNotExist error.
My html is nest in views/ with the core template being views/template.html.
I have tried:
myapp/views/template.html
views/template.html
templates/views/template.html
<full path to app>/<all variations>
None of which can locate the template.
I understand the TEMPLATE_DIRS variable may be set at project level but this is app-specific and the views/ directory is not purely for templates.
I don't think I should add to the url_patterns since the template shouldn't be accessible directly.
I've got this far without them but need to clean up the app.
Any advice?
Edit:
The only line in my controller currently is:
template = loader.get_template("test/templates/test/template.html")
The line has been tested with every variation mentioned above and the one from the first comment.
There are no additional lines added to the urlpatterns, as I understand it adding to here would open access up to the public via url.
it should be:
template = loader.get_template("test/template.html")
Django tutorial suggests to organize templates like this:
Within the templates directory you have just created, create another directory called polls, and within that create a file called index.html. In other words, your template should be at polls/templates/polls/index.html. Because of how the app_directories template loader works as described above, you can refer to this template within Django simply as polls/index.html.
Tutorial also tells what happens if I just put index.html in polls/templates:
Now we might be able to get away with putting our templates directly in polls/templates (rather than creating another polls subdirectory), but it would actually be a bad idea. Django will choose the first template it finds whose name matches, and if you had a template with the same name in a different application, Django would be unable to distinguish between them. We need to be able to point Django at the right one, and the easiest way to ensure this is by namespacing them. That is, by putting those templates inside another directory named for the application itself.
My assumption was that as long as I'm creating an isolated application, it's templates are also isolated from other apps without any subfolder magic.
Is there another way to namespace, so I can refer to template as index.html without creating duplicated folders?
The way templates are organised in a Django project is very flexible. So there's nothing to stop you ordering the template files however you want, as long as you specify the correct template name in the view. For example, you could use a Class Based View for the list of Polls, specifying the template_name as whatever you like:
# polls/views.py
from django.views.generic import ListView
from .models import Poll
class PollList(ListView):
model = Poll
template_name = 'any/path/you/choose/any_name.html'
Having said that, it's good to create a consistent template structure to keep the code maintainable. I'd recommend at the very least keeping your page-level templates (things which are used to return the whole html page) structured in folders with their relevant app name, and anything you include (snippets included with the {% include %} tag) within includes subfolders, like so:
templates
- base.html
polls
- poll_list.html
includes
- poll_votes.html
Edit:
If you still need namespaces despite this, you could use URL namespaces.
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
In my Django app i have the usual django.po locale file under each of the languages. However, I'd like to create a custom locale file called custom.po. Is there any way I could Django to first check for a translation string in the custom.po file and if it doesn't exist, check the django.po file
Thanks.
Check the LOCALE_PATHS setting. This tuple is order-specific, so if you put the path to your custom.po first, you will achieve the effect you're after.
Edit: Additional link for detailed information about How Django discovers translations.
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.