Django admin custom template tag - django

I try to customize my admin panel. I have copied change_list.html to the proper subfolder which is templates -> admin -> model -> change_list.html
While I customize the change_list.html I'd like to use a templatetag but I am not sure where should I put my custom template tag library.
When I put it under django/contrib/admin/templatetags/ it works fine but I want to keep it in my own project tree.
Do you have any idea ?
Note: I have also load my template tag in change_list.html as
{% load adminmedia admin_list i18n grp_tags myproject_tags %}
Thanks

Do not modify or add anything to directory containing Django (do not modify Django!). Keep everything in your project directory (like in the manual).
Admin templates are exactly the same as non-admin templates and you use custom template tags exactly the same way. Put your template tags in yourapp/templatetags/ directory. If your app is in the settings.INSTALLED_APPS that you can load it's tags by passing the module name to the load tag. It accepts also package.module syntax, so: {% load somelibrary %} or {% load package.otherlibrary %}

Related

loading custom tag filters from another app

I am really confused as to how could I load a custom tag filter from another app.
I have a similar problem like this Load custom template tag from another application?
And, I am doing it the same way, but still it doesnt load up and I am getting this error :
TemplateSyntaxError at /
'fillme_tag' is not a valid tag library: Template library fillme_tag not found, tried django.templatetags.fillme_tag,django.contrib.staticfiles.templatetags.fillme_tag,fillme.templatetags.fillme_tag
I have the app in settings installed app too.
I have tried loading it using various ways as mentioned below:
{% load fillme_tag %}
{% load fillme.fillme_tag %} #filleme is appname.
The structure is as follows:
my_project:
app1:
templates:
index.html (this is where i want to load custom tag)
views.py
__init__.py
fillme:
templatetags:
__init__.py
fillme_tag.py (the tag lib)
__init__.py
----- contents of fillme_tag.py ----
from django import template
register = template.Library()
#register.filter(name='demotag')
def demotag(value):
return value
It seems you missed fillme/__init__.py. Add it and this must work:
{% load fillme_tag %}
UPDATE
As error message said it couldn't open fillme_tag as it was invalid Library. My guess is you have a typo somewhere.

Django CMS - Call a cmsplugin in a template tag

My problem
With Django CMS 2.3.3, when creating a Page I use cmsplugin_picture* next to a couple of other cmsplugins. In my cms template, instead of doing:
{% placholder "content" %} //calling the Django Page including all plugins...
I would like to call each cmsplugin seperately, but how would I do that?
I looked at Django tag template (filters) here and also studied Django CMS template tags here, but neither seem to suggest that possibility. I have to say I am a beginner so I might not have connected the dots...
What I try to achieve:
In my template I have a IMG tag (outside of the {% placeholder "content" %} tag) which I want to populate with an image url that I define in my Page/cmsplugin_picture. So I am looking for a placeholder tag that allows me to grab that image. In my wildest dreams I would name it:
{% show_placeholder "content" request.current_page.get_cmsplugin_picture %}
Obviously the above doesn't work, but does something like this exist?
**I have also tried cmsplugin_filer, but to me it isn't necessarely more beneficial to fix this particular problem.*
EDIT:
What I mean by Page/cmsplugin_picture -> In a Django CMS Page you can select between your installed cmsplugins to add to a Page. In my case I select cmsplugin_picture and upload an image (within that plugin). This image I want to 'call' in my Django Template. So it is a not a static url, but dynamic.
You should make a second placeholder where your img tag is (and optionally limit the types and amount of plugins using CMS_PLACEHOLDER_CONF (http://docs.django-cms.org/en/2.3.3/getting_started/configuration.html#cms-placeholder-conf).

How do I load sorl's thumbnail template tag in all my templates

Given that sorl isn't an app directory wide and the template tag definition lives in the virtualenv dir
I want to be able to use {% thumbnail .... %} without having to use {% load thumbnail %} first. It can't be loaded in the layout apparently.
I know it is know critical but it would be nice %}
Copy paste the following code to an init.py that gets loaded,
from django import template
template.add_to_builtins('sorl.thumbnail.templatetags.thumbnail')
I don't think it's possible.
"When you load a custom tag or filter library, the tags/filters are only made available to the current template -- not any parent or child templates along the template-inheritance path."
So you'll have to declare {% load thumbnail %} in every template that uses the thumbnail tag.

Django Templatetag loading

Quick question guys,
Just say I have the code below:
{% for i in c.targetItems %}
<tr> {% include "transfers/matching/_process_match_format.html" %} </tr>
{% endfor %}
In the "_process_match_format.html" I am using a custom template tag. I have to load it in this inclusion file rather then it's parent page otherwise it doesn't seem to be available. Does django only load the custom tag once or does it load it on every pass of the loop?
Additionally, is there way to load the tag in the parent page and make it available for any includes?
According to Django docs: "This means that there is no shared state between included templates -- each include is a completely independent rendering process.". Seems it will load tags each time include is called.
There is a way to load tags for all templates, you need to add them to built-in template tags: Load a Django template tag library for all views by default

Django - Admin - Mandatory fields with ' * '

At present, Django admin will show all the mandatory fields with a bold labels. Is it possible mark with * in the label instead of bold labels?
The Django admin uses templates to render the add/edit page for a model. It is possible to replace that template with one of your own (which extends from the original template) overriding the template blocks you need to in order to make the changes you want to.
Check out the Django docs regarding overriding admin templates for more information.
It's the admin/change_form.html template which you would need to alter in some way (since this template renders the page shown when you add a new instance or edit an existing one). The existing templates already apply a required class to the appropriate labels, so I would create a new template which looks like this:
{% extends "admin/change_form.html" %}
{% block extrastyle %}
{{ block.super }}
<style type="text/css">
/* add an asterisk using CSS */
.required:after {
content: " *";
}
</style>
{% endblock %}
Apply to a Single Model
You should use a model admin class if you want this template to be used for specific models, setting the change_form_template attribute, as described in this section of the docs to the location of the template file you have created.
Apply to a Single App
If you want template to apply to models in an entire app create a templates folder inside the root of the app. Django will automatically look for templates there, so if you create a folder called admin and place a file in there called change_form.html it will automatically override the default Django template of that name (admin/change_form.html).
Project Wide
In order to apply this template project wide create a folder somewhere (not inside an app) called templates. Again place your new template in this directory at admin/change_form.html.
Next edit the template directories Django setting specifying the location of this directory in order to allow Django to find the template and override the default templates in the same way as before only project wide and not just app wide.
This is quite a complex set of things to do, especially for such a simple change and you may find it tricky if you have not worked with admin templates before (or even if you have).
Hopefully you now understand what is required to change an admin template, its actually fairly elagant (as is Django) but in my opinion not worth the effort just to change to some asterisks.