I'm working with Django and in the base template I see this: {% get_cms_html_fragment "html/footer" %}
From the surrounding syntax I can easily conclude that this is a footer include of some sort. But, it doesn't follow the traditional syntax of {% block headerexample %}{% endblock %} that is normally used in Django templates. Does anyone recognize this?
In Django you can easily create custom tags. So it can be this very project specific tag.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/
This is a syntax for a template tag. FYI, block is one of the built-in template tags, get_cms_html_fragment is a custom one.
Custom template tags and filters are loaded via load built-in template tag.
See also:
Django: what does "load" do (in a template file)?
Related
Is there a way to edit the details view model in flask-admin? I have searched the very bad docs and I couldn't find a way with which I would be able to display the model's details in a customized way!
Is there any reference to how the "details_view" should be used, assuming this is how I modify the details view?
If not, can anyone please explain to me how can I modify the way the info is displayed in that list? I have a "list of tags" column, and I wanna show tags separately based on certain criteria, I wanna apply some filters for example before showing them. How would I do that?
custom detail view can be acheived by :
1) setting the template of the modelView you are trying to customize :
class MyModelView(AdminModelView):
details_template = "admin/details.html"
2) Edit your custom template admin/details.html by totally overiding it with a whole new page. I guess you want to add additional information or custom fields most of the time, so you can start with a admin/details.html page that looks like :
{% extends 'admin/model/details.html' %}
{% block tail %}
{{ super() }}
<h1>My custom content.</h1>
{% endblock %}
By inheriting from parent template, you should have environment variable available in the template.
You may find more about available override options and such there : http://flask-admin.readthedocs.io/en/latest/api/mod_model/#flask_admin.model.BaseModelView.details_template
I have problem with one idea (dynamic sort template in django)
Now I have simply solution for sorting my data but it is not so good because I have couple of buttons which adnotate to sorting view and in this way I have sorted list of my data.
I tried to use angularjs, I think this is the best way to do it but I have problem too. For example:
<div ng-app="" ng-init="sort_by = 'updated_at'">
<button ng-click="sort_by = 'id'">id</button>
<button ng-click="sort_by = 'created_at'">created_at</button>
<button ng-click="sort_by = 'issuer'">issuer</button>
<button ng-click="sort_by = 'handler'">handler</button>
<p>{% verbatim angular %} {{sort_by}} {% endverbatim angular %}</p>
{% for fault in faults|dictsort:{{sort_by}} %}
in theory it can works. but not.
I try in this way and in apostrophe and I try with another block like above {% verbatim angular %} and this is not working giving me an error like this:
'for' statements should use the format 'for x in y': for fault in faults|dictsort:'{% verbatim angular
so it is even not recognizing angular block.
I have to ask You for help and it doesn't have to be angular, if it is better idea to do this in template or maybe stricte django way??
Thanks for help me :)
Why you type ng-app="" ? You should put here name of your AngularJS application. Did You create your AngularJS module first?
You can sort both in frontend and backend, but You should do this in frontend - it is the job of web browser - backend should be responsible only for generating data to be consumed for frontend.
Something like this {% for fault in faults|dictsort:{{sort_by}} %} can't work. You try to define an AngularJS variable called sort_by, and then use it in Django filter. Django doesn't know nothing about that variable.
Both Django and AngularJS use {{}} for variable bindings in templates. You can change these symbols for AngularJS or use verbatim and endverbatim tag in Django.
I want to know children templates on which depend on parent template in Django. I need to know all filenames or modification dates. How can I check it in Django.
For example children like {% extend %}, {% include %} and so on.
Since {% extends %} and {% include %} accept also variables as parameters, there is no bulletproof way of generating a template inheritance tree unless those variables have deterministic values. After all, those variable values can change in a way that the code can't predict.
If you're using only static extends and includes, then I imagine a regular text search is your best friend. You can do:
find . -name "*.html" -print | xargs grep "{% extends "
in your project folder to generate a list of all extends tags, and then later you just need to parse the output. Same for the include tag.
For a different solution, I imagine you could add some debugging code in the Django template engine and traverse the whole site or specific pages - this way you could build such a tree, but only for views and conditions that you define.
I know Django has already a good templating system for designers but I wonder if it is possible to limit the usage of certain template tags and filters.
We are building a Django plugin for designers to make template developing more open, but we want to hide some logic of django template system and expose just the necessary to the designer.
In example: How can I prevent the use of {% load %} template tag and preload only the tags that I want?
Try this decorator: Safe template decorator
From author description:
A decorator that restricts the tags and filters available to template
loading and parsing within a function.
This is mainly meant to be used when granting users the power of the
DTL. You obviously don't want users to be able to do things that could
be potentially malicious.
The {% ssi %} tag, for example, could be used to display sensitive
data if improperly configured.
{% load %} gives them access to all the unlimited python code you
wrote in your templatetags. {% load sudo %}{% sudo rm -rf / %} o_0
Note that the "load" tag (among others) is not listed in the default
tag whitelist. If you parse a template (however indirectly) in a
function decorated with this, unlisted builtin tags will behave like
undefined tags (ie, they will result in a TemplateSyntaxError).
Since {% load %} is not whitelisted, you may want to include some
custom tags or filters as "builtins" for convenience. Simply put the
module paths to the libraries to include in the extra kwarg or the
extra_libraries list. Generally, this is not recommended, as these
libraries need to be carefully and defensively programmed.
NOTE: This does not do anything about cleaning your rendering context!
That's completely up to you! This merely restricts what tags and
filters are allowed in the templates.
Examples:
from django.template.loader import get_template
safe_get_template = use_safe_templates(get_template)
tmpl = safe_get_template('myapp/some_template.html')
from django.template import Template
use_safe_templates(Template)('{% load sudo %}')
# TemplateSyntaxError: Invalid block tag 'load'
An easy way would be to implement your own template loader similar to Django's file system loader and strip certain tags out of the text (guess you could event turn into a template/template nodes when doing so to be able to parse it correctly) before giving the template to Django for further processing.
Is there a Django equivalent to Mako's callable blocks?
In Mako, if I have a particular bit of HTML that I would like to reuse, I can put it in a "callable block" which can be called from multiple places in the template.
<%def name="makerow(row)">
<tr>
% for name in row:
<td>${name}</td>\
% endfor
</tr>
</%def>
Does Django have something similar to this?
In Django you can include other HTML templates to accomplish this:
{% include "main/includes/subtemplate.html" %}
You can access the same variables in the included template as in the parent template from which it is included. This allows you to reuse HTML in multiple places in a template.
An alternative is to create a custom template tag: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ This allows you to create your own tags that generate HTML output. The Django documentation provides various examples on how to do this.