I'm used to auto-format my sources with eclipse. To do so I select all text and press Ctrl+Shift+F.
But In the case of django template files as for example
{% extends "base.html" %}
{% load staticfiles %}
{% block content %}
<div class="row">
...
by Ctrl+Shift+F I would end up with
{% extends "prototipo/base.html" %} {% load staticfiles %} {% block
content %}
<div class="row">
which leads to a TemplateSyntaxError.
Are there any workarounds?
In the meanwhile I developed this script, positioned in the tamplates dir. I run it after a Ctrl+Shift+F.
import re
import os
def fix_template(filename):
with open(filename,'r+') as f:
content=f.read()
for s in re.findall('(\{%((?!%}).)*\n.*%})',content):
faulty_string=s[0]
non_faulty_string=faulty_string.replace('\n',' ')
content=content.replace(faulty_string,non_faulty_string)
f.seek(0)
f.write(content)
basedir=os.path.dirname(os.path.abspath(__file__))
for dirpath, dirnames, filenames in os.walk(basedir):
for filename in filenames:
if re.match('.*[.]html',filename) is not None:
fix_template(os.path.join(dirpath,filename))
Humm, that editor is from LiClipse (and not PyDev) right?
I think the problem there is that it's using the default HTML code-formatter, whereas it should use a django-templates aware code-formatter.
Please report that as an issue to LiClipse (https://sw-brainwy.rhcloud.com/tracker/LiClipse) -- although note that the LiClipse way of doing things is delegating those actions to external libraries (so, HTML formatting works because it was available in an external library -- so, in this case, the option could either be disabling formatting or finding a library which implements it).
Related
Problem:
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block pagetitle %}
becomes
{% extends 'base.html' %} {% block title %} Dashboard {% endblock %} {% block
pagetitle %}
Note that the {% tag %} is being broken with a new line. This causes syntax errors with django templates.
I've tried most top django template extensions and this does not fix the issue.
I've also tried these settings:
"[html]": {
"editor.formatOnSave": false,
},
"html.format.wrapLineLength": 0,
"html.format.enable": false,
"prettier.disableLanguages": ["html"]
Desired Behavior:
Automatically format *.html files, while preserving django template tags, not breaking them up with newlines.
Sub-optimal (but acceptable) behavior: don't format *.html files at all.
I had the same issue and the only way I found that solved it is to disable the default HTML formatter. Unfortunately, I did not find a way to make it format Django template tags correctly. You can do the same if you go to VS Code Preferences > Settings > User > Extensions > HTML and uncheck 'Enable/disable default HTML formatter'.
I solved this by following this advice: https://stackoverflow.com/a/73892745/1257347
TLDR: install the djLint extension (and remember to do $ pip install djlint)
I got it to work by simply adding {{""}} between the {% tag %} that were being broken.
Example:
{% extends 'main/base.html' %} {% block title_block %}Homepage{% endblock%}
{{""}} {%block style_ref_block%}{%endblock%} {{""}} {% block body_block %}
This Didn't work for me.
The hack I found was to set the vscode language to jinja instead of the auto detected html
reference
I've also just experienced vs-code misbehaving on django template tags (i.e. deleting curly braces).
I don't like the idea of disabling HTML formatting just to support templates (i.e. vs-code Preferences/Settings/Extensions/HTML: disable (uncheck) "HTML>Format:Enable"). This is arguably a step backwards, but it does stop vs-code misbehaving.
Instead, I chose to install (vs-code Preferences/Extensions) the 'Django' extension, by Baptiste Darthenay. This was a better way to go, because it works, gracefully, preserves native vs-code HTML formatting, and includes a nice set of django snippits, which saves me keystrokes when embedding template code. Tada!
BTW, before finding Baptiste's awesome extension, I also tried keeping vs-code HTML formatting enabled, AND enabling 'HTML>Format:Templating', which promised to "Honor django and other templating language tags"; it did not.
So on my front-end, I want to show some HTML only if the user belongs to one of 2 groups: 'admins' or 'clerks'. There are 3 groups of users: 'admins', 'clerks', and 'sellers'. Here is my front-end code:
{% if user.groups.all.0 == "admins" %}
<h1>Some HTML</h1>
{% elif user.groups.all.0 == "clerks" %}
<h1>Some HTML</h1>
{% endif %}
When I run this code, the HTML shows for the admins. But that of the clerks does not show. I have tried printing out the group to be sure that the spelling and the casing were the same, and they were the same. And Django does not throw an error. It only works if I rewrite the code as follows:
{% if user.groups.all.0 == "admins" %}
<h1>Some HTML</h1>
{% elif user.groups.all.0 != "sellers" %}
<h1>Some HTML</h1>
{% endif %}
But I feel like this is not good design. Please am I missing something? Thank you all in advance
First of all, is a not practical idea for too many reasons.
Think that using that method you are getting possibilities and processing that don't correspond to the templates. The templates engine philosophy is just basic and no important logic.
A way to have more control and more practical is using a flag. For example
groups_permited_for_this=["admins","clerks",...]
Permited =False
for group in user.Groups.all:
if group.name in groups_permited_for_this:
Permited =True
Pass it as context and then use a Jinja if statement.
So I resolved the issue in a slightly different way from the one above:
First, I created a folder called 'templatetags' inside the app.
Then I created 2 files in it:
An empty 'init.py' file
A 'cust_auth.py'(for custom authentication) file
Inside the 'cust_auth.py' file I wrote a function that does a similar thing as in #Paulo Aguilar's answer above:
from django import template
from django.contrib.auth.models import Group
register = template.Library()
#register.filter(name='has_group')
def has_group(user, group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
Then in my template, I loaded the 'cust_auth.py' file at the top:
{% load auth_extras %}
Then I did the logic I was looking for:
{% if user|has_group:"admins" %}
<h1>Some HTML</h1>
{% endif %}
I preferred this because I think over the cause of my application, I will want more custom authentications
Thank you Paulo and whoever posts
I have a Django website that I'm trying to get internationalized. The image files are all encoded with the language code of two letters. So when a user switches the language, the references to image files are updated with the language code in templates as follows:
<img src="{% static 'website/images/contact_us_{{ LANGUAGE_CODE }}.png' %}">
Problem is, I have to have a tag as well for the path of static content. What is an elegant way to solve this?
Per #MarAja suggestion, I followed his/her question and solution here which was practically identical to mine. I'm posting what I did, so whoever that lands on this page has a solution. During my research, I did not stumble upon #MarAja's post.
The code is an exact copy, and the choice not to use add tag is because according to the Django documentation, it tries to cast the arguments to an int i.e. not intended for strings.
Full code:
# Custom Template tag file named "custom_app_tags.py"
from django import template
register = template.Library()
#register.filter
def addstr(s1, s2):
return str(s1) + str(s2)
Finally, usage:
{% load staticfiles %}
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
{% load custom_app_tags %}
<img src="{% static 'website/images/contact_us_'|addstr:LANGUAGE_CODE|addstr:'.png' %}">
Note, I included everything so that whomever gets here later, gets a complete picture of what is going on.
I need a javascript templating system and i think handlebars.js does an excellent job in this case.
I'm having syntax conflicts with handlebars templates inside a django template because django tries to render handlebars variables.
Is there a tag in django templates to stop rendering a block with curly braces?
Something like:
{{ django_context_varable }} #works
{{% raw %}}
<script id="restaurants-tpl" type="text/x-handlebars-template">
<ul>
{{#restaurants}} #not rendered by django, plain text
<li>{{name}}</li>
{{/restaurants}}
</ul>
</script>
{{% endraw %}}
Edit
Likely i found this. It works fine.
Update
Django 1.5 supports verbatim tag natively.
I use a custom template tag for another js templating system, here:
https://gist.github.com/629508
Use in template:
{% load mytags %}
{% verbatim %}
{{ This won't be touched by {% django's %} template system }}
{% endverbatim %}
Edit: This custom template tag is no longer necessary, as Django's template language now supports the {% verbatim %} template tag.
Is there a tag in django templates to stop rendering a block with curly braces?
OLD Answer for Django 1.0-1.4: No, though you could though you could put the block in a separate file and include it without rendering or use a different templating engine.
New Answer: The answer above was correct in August 2011 when the question was asked and answered. Starting in Django 1.5 (released Feb 2013, though alpha/beta versions in late 2012), they introduced the {% verbatim %} and {% endverbatim %} which will prevent the django template engine from processing the content in the block.
So for the question asked the following will work in django 1.5+ out of the box:
{{ django_context_varable }} #works
{% verbatim %}
<script id="restaurants-tpl" type="text/x-handlebars-template">
<ul>
{{#restaurants}} #not rendered by django, plain text
<li>{{name}}</li>
{{/restaurants}}
</ul>
</script>
{% endverbatim %}
The documentation on verbatim is here. Yes, this was noted by others earlier, but as this is the accepted answer I should list the easiest solution.
I wrote a very small django application : django-templatetag-handlebars exactly for that purpose.
{% load templatetag_handlebars %}
{% tplhandlebars "tpl-infos" %}
{{total}} {% trans "result(s)." %}
<p>{% trans "Min" %}: {{min}}</p>
<p>{% trans "Max" %}: {{max}}</p>
{% endtplhandlebars %}
Render your block as usual using Handlebars.js API :
var properties = {
total: 10,
min: 5,
max: 4
};
var template = Handlebars.compile($('#tpl-infos').html()),
rendered = template(properties);
I wrote it the day #chrisv published its package, with a KISS approach in mind. It is mainly based on Miguel Araujo's gist : https://gist.github.com/893408.
for a deeper integration between handlebars and Django (including optional on-the-fly precompilation) check out my project at
https://bitbucket.org/chrisv/django-handlebars/
It basically works like this:
create HB template under
appdirectory/hbtemplates/myapp/template.html
(just like Django template)
in your app, use
{% handlebars myapp %}
template tag and render template like so:
Handlebars.templates["myapp.template.html"]({context:"value"});
Compile your handlebars first!
From handlebars precompiling documentation:
In addition to reducing the download size, eliminating client-side compilation will significantly speed up boot time, as compilation is the most expensive part of Handlebars.
You can compile templates in your build environment using handlebars npm module, or integrate it with a build tool like gulp with gulp-handlebars.
After compiling, your handlebars templates can be served as static resources and bypass server side rendering altogether. Makes it easier on caching too :)
Typical usage would look like this:
<div id="restaurants-tpl">
Waiting for content...
</div>
<script src="{% static 'js/handlebars.runtime.js' %}"></script>
<script src="{% static 'js/templates.js' %}"></script>
<script>
// Let django render this as a json string
properties = {{ properties }};
// Use Handlebars compiled template imported above
rendered_html = Handlebars.templates["restaurants-tpl"](properties);
// Set element content
document.getElementById("restaurants-tpl").innerHTLM = rendered_html;
</script>
Django's templating system doesn't support escaping blocks at a time. It would be easy to work around were it not for the fact that when templates are processed the tokenizer doesn't keep exact information on what the tokens looked like before they got tokenized.
I have used the following work-around which is ugly, but (sort of) works. Use different tag delimiters in your templates and a django template tag that translates those back to what you actually want:
#register.tag(name="jstemplate")
def do_jstemplate(parser, token):
while self.tokens:
token = self.next_token()
if token.token_type == TOKEN_BLOCK and token.contents == endtag:
return
self.unclosed_block_tag([endtag])
nodelist = parser.parse( ('endjstemplate',) )
parser.delete_first_token()
s = token.split_contents()
tmpl_id = Variable( s[1] ) if (len(s) == 2 and s[1]) else ''
return JsTemplateNode( nodelist, tmpl_id )
class JsTemplateNode(template.Node):
def __init__(self, nodelist, tmpl_id=''):
self.tmpl_id = tmpl_id
self.nodelist = nodelist
def render(self, context):
content = self.nodelist.render(context)
return u'<script id="%s" type="text/x-handlebars-template">%s</script>' % (
self.tmpl_id.resolve(context),
re.sub( ur'{\$(.*?)\$}', u'{{\\1}}', content ), )
For bonus points you can leverage Django's templates within your templates ...
which will probably cook your brain trying to untangle later:
{% jstemplate "restaurants-tpl" %}
{$#restaurants$}
<div id="<$name$<" class="{$type$}">
<ul class="info">
{$#if info/price_range$}<li><em>{{ trans "Price Range" }}:</em> {$info/price_range$}</li>{$/if$}
{$#if info/awards$}<li><em>{{ trans "Awards" }}:</em> {$info/awards$}{$/if$}
</ul>
<div class="options">
<button>{% trans "Reservation" %}</button>
</div>
</div>
{$/restaurants$}
{% jstemplate %}
Actually I wrote a custom template filter which goes like this:
from django import template
register = template.Library()
def handlebars(value):
return '{{%s}}' % value
register.filter('handlebars', handlebars)
and use it in a template like so:
{{"this.is.a.handlebars.variable"|handlebars}}
It's the simplest thing I could think of. You just have to put your handlebars variable name in quotes. I regret I hadn't got this idea before I struggled with ssi. It works also with keywords:
{{"#each items"|handlebars}}
Why not use jinja2 instead? IMO, they're both elegant to use. Here's an excellent article about it: Using Jinja2 with Django
I'm trying to build my own template tags.
I have no idea why I getting these errors. I'm following the Django doc's.
This is my app's file structure:
pollquiz/
__init__.py
show_pollquiz.html
showpollquiz.py
This is showpollquiz.py:
from django import template
from pollquiz.models import PollQuiz, Choice
register = template.Library()
#register.inclusion_tag('show_pollquiz.html')
def show_poll():
poll = Choice.objects.all()
return { 'poll' : poll }
html file:
<ul>
{% for poll in poll
<li>{{ poll.pollquiz }}</li>
{% endfor
</ul>
in my base.html file im am including like this
{% load showpollquiz %}
and
{% poll_quiz %}
Bu then I get the the error:
Exception Value: Caught an exception while rendering: show_pollquiz.html
I have no idea why this happens. Any ideas? Please keep in mind I'm still new at Django
Shouldn't all custom filters be inside the templatetags directory?
templatetags/
__init__.py
showpollquiz.py
then
#register.inclusion_tag('show_pollquiz.html')
looks in MY_TEMPLATE_DIR/show_pollquiz.html for the template
You forgot to close your template tags... Also, you should change the name in the for tag, you can't have for poll in poll:
<ul>
{% for p in poll %} <!--here-->
<li>{{ p.pollquiz }}</li>
{% endfor %} <!--and here-->
</ul>
Also notice you're not using the inclusion tag you defined at all. I think you got some code mixed up, try to go over a tutorial start to end and things will be clearer.
I'd not bother with writing your own template tags: take things one step at a time and stick to the basics for now. There's nothing wrong with {% include 'show_pollquiz.html' %}
I found the problem. The problem was that the #register.inclusion_tag('show_pollquiz.html')
inclusion tag is obviously looking for the template in the default_template directory. So that is why i got the error.
According to me this is not clear in the documentation. But I guess its how it is, being a template and all...
Oh , well.
Now, how would I put the #register.inclusion_tag('show_pollquiz.html') to look in the same folder as the app? under templatetags/?