Blank spaces inserted in Django template empty div - html

I have a Django template which looks like this:
<div class = 'description' contenteditable = 'true' placeholder='{{my_placeholder}}'>
</div>
The problem is that Django inserts blank spaces in div so that the code below yields
A
B
instead of AB:
alert('A' + $('.new').find('.description').html() + 'B');
If I rewrite the HTML code as follows (by bring </div> on the same line):
div class = 'description' contenteditable = 'true' placeholder='{{my_placeholder}}'></div>
...the issue goes away. However, I do like the HTML structure where the closing is on a new line. How can I keep this structure so that the element is really blank and I can see the placeholder value ?

You can use {% spaceless %} to remove whitespace between HTML tags:
{% spaceless %}
<div class = 'description' contenteditable = 'true' placeholder='{{my_placeholder}}'>
</div>
{% endspaceless %}
More about {% spaceless %} here.

Solution with spaceless template tag is fine if you've got only few spots with urgent need to eliminate whitespaces, but you should not have too much of them or wrap entire content in spaceless.
My point is that it's JavaScript work to be aware that content might contain whitespaces, so just use something like trim() function for inner HTML:
alert('A' + $('.new').find('.description').html().trim() + 'B');
Other than that, indented code with newlines like you've got is perfectly fine for readability purposes, however, I suggest minifying it on production with tools like django-htmlmin that will squash your templates when it reaches the user.

Related

how do I fix the twig filter from affecting html tags inside?

I'm learning Drupal8 and Twig with Chaz Chumley's book 'Drupal 8 Theming with Twig'.
When I put in the code provided I don't get the desired result. (Chapter 3, Filters)
The book says to add the following to the page.html.twig file:
{% filter upper %}
<p>{{ name }} is the best cms around.</p>
{% endfilter %}
but the page outputs
<P>DRUPAL IS THE BEST CMS AROUND.</P>
(Showing the html tags on the page as shown here)
Is there something I'm missing to have the twig filter not change the HTML tags? or is the only solution to put the filter inside the tag? but this filter is supposed to "wrap sections of HTML and variables" so why is it affecting HTML tags?
You can put the filter around just the text, so it ends up as:
<p>{% filter upper %}{{ name }} is the best cms around.{% endfilter %}</p>
You can test your twig code here: https://twig.stapps.io/
Can you give this a try:
<p>{{ 'your text'|upper }}</p>
Also, check out https://drupal.stackexchange.com/ if you have any more drupal related questions.

Django Templates: How to display html code?

I'm trying to display some HTML markup in a blog, and would like to know if there is a way to wrap a section of my Django template directly, without putting it into a context variable.
For example, I would like to output a bunch of code, some of it JavaScript, and some of it HTML, and some of it CSS. If I enter in the code directly into my Django template, and wrap it in some pre tags:
<pre>
/* Here is the markup I want to display: */
... lots of HTML
</pre>
the HTML tags are rendered.
Of course, to display:
<
I should use
<
and to display
>
I should use
>
I tried adding the Django tag {% autoescape on %} around the code section, but it had no effect because I'm not rendering a context variable.
I would like to know if there is an easier way than replacing every occurrence of < with < and every occurrence of > with >
I also know that if I put the code that I want to display into a context variable, then in my template, just displaying that context variable would automatically escape the code.
But I would rather just be able to directly cut and paste the code I want to display into my template. Is there a way to do this and display the HTML tags (i.e.
<h1> Heading Level 1 </h1>
without writing it in my template as:
<h1> Heading Level 1 </h1>
You have to use xmp tags.
<xmp>
<h1>Testing Html</h1>
</xmp>
This question is old, but it pops on search engines and no answer is correct imo.
SÅ‚awek Kabik's is deprecated, Smit Patel's is overly complicated (it bloats views).
In order to do what OP asked for, you have to use the force_escape built-in filter in a {% filter %} tag.
Example:
<pre>
<code>
{% filter force_escape %}
<span class="hello">Anything HTML really</span>
{% endfilter %}
</code>
</pre>
Output:
<pre>
<code>
<span class="hello">Anything HTML really</span>
</code>
</pre>
NOTE: Before you try to implement manually, please have a look at ckeditor.
ckeditor documentation
if this is not what you are looking for, then only proceed with answer.
Just Wrap your variable inside following Django template tag.
{% autoescape off %}
{{your_variable_here}}
{% endautoescape %}
put HTML code in "your_variable_here" variable And Django Will Display It as HTML. All HTML Tags will Work.
EDIT:
Sorry, I missed important part to mention.
in views do this
from django.template.loader import render_to_string
rendered = render_to_string('my_template.html', {'foo': 'bar'})
and pass this rendered string to template variable and render the given template inside other template by putting lines
{% autoescape off %}
{{rendered}}
{% endautoescape %}

Django - <ul> content from db is not rendered as HTML, is pure text

Not sure what is wrong here. I have some html content in my tables.
Table column:
(ul)(li)one(/li)(li)two(/li)(li)three(/li)(/ul)
Template:
(div class="plst")
(p)Pros:(/p)
{{ review.plist }}
(/div)
Please replace the () with <>
In the output i am seeing
(ul)(li)one(/li)(li)two(/li)(li)three(/li)(/ul)
as such and not
onetwothree
Don't know what is happening here. Thanks for your time.
You need to apply the safe template tag filter.
<div class="plst">
<p>Pros:</p>
{{ review.plist | safe }}
</div>
The safe filter turns off escaping of <, >, and &. With escaping off, they are passed straight through to the output.

Django passing HTML objects into template as plain text

This feels like a really silly issue, but I'm confused by the behavior of django-zinnia, a blog creation module.
When I test enter a plain text post, it appends each sentence with html < p > tags the browser doesn't read as html.
Example, if I enter this into the database (no html):
The entry from the db renders on page itself like this as if the < p > markup was plain text:
Within Zinnia, these html tags are being generated as part of the {{ object_content }} object in _entry_detail_base.html
<div class="entry-content">
{{ object_content }}
</div>
I've looked through the entry.py models within Zinnia and I'm having trouble identifying where these tags are coming from or how they're being passed in in a way the browser doesn't interpret them for what they are (html). Is there a filter I can apply that might solve this? thanks
That's the default behavior for Django templates. Use {{ object_content|safe }} or {% autoescape off %} {{ object_content }} {% endautoescape %} (for multiple variables) to prevent html entities from being escaped.
Note that using the safe filter doesn't automatically mean the output is not escaped if you use another filter after it.
Check the Zinnia's source code: https://github.com/Fantomas42/django-blog-zinnia/blob/master/zinnia/templates/zinnia/_entry_detail_base.html
It's using |safe template tag:
<div class="entry-content">
{{ object_content|safe }}
</div>

Django's template variable gets interpreted as HTML

When DEBUG=True a variable containing HTML is just printed in the template and not interpeted as HTML. This is the correct behavior.
On the other hand, when DEBUG=False (just changing this) the variable's content is interpreted as HTML and I cannot understand why. I'm not using the safe filter.
My template is as follows:
<pre id="copy-source-{{ forloop.counter }}">
<code>{{ code }}</code>
</pre>
In one case, code happens to be HTML code, specifically:
<img src="bblabla" />
and the page displays the image instead of the code, even if it's inside a pre-code!?
Try this
{% autoescape off %}
{{var_containing_html}}
{% endautoescape %}
I hope this will help.
Good luck.
P.S. Also see docs