Change vim indenting format - django

I want to add to the way html is indented in vim. I'm doing django development and I would like to indent whenever a django template tag is used. Currently, using filetype indent, it does not indent after the template tags. So currently my code looks like this:
{% do_something %}
<div>
<p>Hello</p>
</div>
{% end %}
And I'd like for it to recognize the {% %} as a tag and indent like so:
{% do_something %}
<div>
<p>Hello</p>
</div>
{% end %}
Is there a filetype plugin for this or a way I can add {% %} to the list of things that should be indented after?

When you have filetype indent on for an html file it will use the indenting rules found in the ../vim/vim73/indent subdirectory in file html.vim.
The braces you want to use as signaling indent of next line are, I'm sure, not treated in html.vim because they're not part of html. You can alter the rules in html.vim to get it done the way you want.
See :h indent-expr for a bit of info and you will also want to look at other files in the /indent directory to see how it works.
There is an alternate html.vim you can get at vim website, maybe it is better than html.vim that ships with Vim:
http://www.vim.org/scripts/script.php?script_id=2075

There is a pending pull request for the django.vim project to include an alternative django-custom vim implementation from Steve Losh. This works, for the most part, better than the default one.

Related

VSCode breaks Django template tags with newline

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.

Jekyll Liquid: use truncatewords filter on a whole included file

Is there a way in Jekyll with Liquid, to include another file and truncate the number of words from it?
{{ {% include intro.md %} | truncatewords: 3 }}
No matter how I set the curly braces, no luck...
Including intro.md alone works:
{% include intro_DE.md %}
Here is a vague note, that include might be a bit different under Jekyll, but I still have no clue...
Perhaps, I need to go through capture?
I found a solution with capture. It just not very elegant:
{% capture foo %}{% include intro_DE.md %}{% endcapture %}
{{ foo | truncatewords: 20 }}
The output is still markdown-ish (=good). “Inline-styles“ like bold, underline, strikethrough still work.
### Headlines and <html tags> don't. (Probably because this technique kills the linefeeds...)
"truncatewords" works only for string datatype,
for example: If we have to use limited words on card description then it is good practice to use truncatewords.
[for more please read this] https://shopify.github.io/liquid/filters/truncatewords/

Preventing XSS attack Django

Currently I am using a CKEditor text editor. It escape's html but I want to prevent it from server side.
What is the best way to prevent XSS When we are using Text Editor in Python/Django?
Automatically or explicitly escape your output in templates, e.g.
{% autoescape on %}
{{ body }}
{% endautoescape %}
or
{{ body|escape }}
If you want to only escape JavaScript the "right" way to do it would be to convert the HTML to DOM, walk the tree of nodes, and remove any script elements. A less elegant and imperfect solution would be to use a regular expression to replace any script tags.

How to disable autoescape in django feeds?

I use django feed framework to organize rss feeds for my website.
I need to put some hyperlinks to feed items, but al of them are
autoescaped ( "<" is replaced with "<" and so on).
Is it possible to keep tags in my feed (as I understand, I can't use
{% autoescape off %} tag in feed templates)?
Thanks.
Read up on Automatic HTML escaping in Django and try the following syntax. Where data is the variable which holds your link
{{ data|safe }}
As jitter mentioned you can use "safe" filter, but it's annoying if you want to disable autoescaping often. Django also supports {% autoescape off %} {% autoescape end %} blocks, everything inside is block won't be autoescaped.
EDITED: Sorry, I haven't read your question completely only title :). Why you can't use autoescape tag in feeds? There's no restriction about it.

Display Django Code from a Django template

I am trying to display Django source code from a Django template. However, I cannot find a tag similar to HTML's pre or xmp.
Here's the code
Also, I have a block with the same name which springs the error.
If your view puts the source code in a context variable called source, your template might look like this:
<pre>
{{ source|escape }}
</pre>
The escape filter will escape certain characters to make sure the HTML is rendered correctly.
If you just want to display hard coded template source in your template, there are two options.
Use HTML escaping to do so and remove your XMP tags.
{ instead of }
} instead of {
Or use the templatetag template tag:
{% templatetag openbrace %} instead of }
{% templatetag closebrace %} instead of {
etc.. refer to link
i don't really sure if i understand:
If you want show django template code try change '{' and '}' to
{ and }
After that django will not recognize it as var.
EDIT: another way to tell django not to parse code is here :) http://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag
Django has a special template tag for this purpose.
use verbatim template tag
{% verbatim %}
...
{% endverbatim %}