Django: Avoid rendering as variable in template - django

I just started learning django for a few day. Django's templating system looks fascinating. but currently I am facing an issue.
I need to print this kind of text in my webpage multiple times through template.
She {{ was }} a very {{ helpful }} and {{ attractive}} girl.
But whenever I try to use this in template, it thinks those words are variable thaI am referring, and vanishes them from the output due to not getting a value. So the output becomes,
She a very and girl.
I completely understand that this is the intended behaviour, but in some case I am trying to tell the rendering engine to render that as it is. Is there any filter or workaround??
[ I have to use them inside the template, and they can't be passed through variables as string]

You can use the verbatim template tag, which will stop rendering what is inside the tag. See docs here.
{% verbatim %}
She {{ was }} a very {{ helpful }} and {{ attractive}} girl.
{% endverbatim %}

Related

Django 1.10 Print values in same line in Templates

i want in same line print two value look like "1. Question ...".
but first {{ }} after set new line. look like this,
"1."
"Question ..."
{% for q in question %}
<p> {{ forloop.counter }}. {{ q.question|safe }}</p>
{% endfor %}
How can i print two value in same line in template ?
I want this:
1.Question
2.Question
...
Based on your comment, you say that q.question is the content of a CKEditor. Often times, these output at least wrap the content inside a <p> tag. In this case, the result output generated by Django would a nested <p> tag inside the <p> from your template:
<p>1. <p>Question</p></p>
This is invalid HTML, but the browser tries to render it as best as it can. I think you can either include the number inside the CKEditor and exclude it from your template or change your field to store a simple CharField, and keep your HTML unchanged.
This depends on the flexibility you want in your application.

Adding an if statement to django-cms template tag

I'm probably missing something very obvious but can't see anything in the documentation. I have a carousel with a and each will hold an image. However I've added 6 but I want to add an if statement so if an Image has not been added you don't see a blank space, where there is no content inside the .
Here is what i've tried so far:
{% if "Carousel 1" %}
<li>
{% placeholder "Carousel 1" %}
</li>
{% endif %}
Attempt 2:
{% placeholder "Carousel 1" as cara1 %}
{% if cara1 %}
<li>
{{ cara1 }}
</li>
{% endif %}
Not sure if there is something differnt i need to be doing for the django-cms template tags?
Any help would be much appreciated. Docs here - http://docs.django-cms.org/en/latest/advanced/templatetags.html#placeholder
Not to be rude, but your approach is way, way off :)
Placeholders hold Content Plugins. Content Plugins are responsible for how they render their contents.
My advice would be to create or find a carousel content type plugin. This plugin will hold multiple images or "CarouselImage" model instances that you can iterate over, and also specify a template with which to render itself.
In this template resides the conditional statement you're wanting to check for. Placeholders are just that - places held for content plugins.

_() or {% trans %} in Django templates?

In Django templates, you can use either {{ _("Hello World") }} or {% trans "Hello World" %} to mark strings to be translated. In docs, the “official” approach seems to be the {% trans %} tag, but the _() syntax is mentioned too once.
How these approaches differ (except syntax) and why should be one preferable rather than the other?
One difference is that you obviously can't use {% trans %} with tags and filters. But does that mean that I can just use _() everywhere, like {{ _("String") }}? It works and looks much cleaner and more consistent than using {% trans "String" %} with standalone strings and _() with tags and filters.
So it seems that there's technically no difference as of Django 1.5. Template engine internally marks a variable for translation (by setting its translate attribute) in two cases:
when you do {% trans VAR %} (see TranslateNode), or
if the name of a variable starts with _( and ends with ) (see Variable.__init__).
Later, when the variable is being resolved, Django wraps it with ugettext or pgettext if it sees the translate attribute.
However, as can be seen from source code, there are some flexibility considerations in favor of {% trans %} tag:
you can do {% trans "String" noop %}, which will put the string for translation into .po files, but won't actually translate the output when rendering (no internal translate attribute on variable, no ugettext call);
you can specify message context*, like {% trans "May" context "verb" %};
you can put translated message into a variable for later use*, like {% trans "String" as
translated_string %}.
* As of Django 1.4.
Please feel free to correct me or post a better answer in case I'm missing anything.
The trans template tag calls the ugettext() function. In Django _() is an alias to ugettext().
This is covered in the django docs.

How do you print out the exact text "{{text}}" in a Django template?

How do you print out "{{text}}" in a Django template? If I type it into a Django html template it gets interpreted as the variable text. I want the actual text:
{{text}}
To appear in the html output.
To output the characters used to compose template tags, you have to use a specific template tag called templatetag. If you want to output the {{ characters, for example, you use {% openvariable %} and the output of that template tag would be {{.
So for your example,
{% openvariable %} text {% closevariable %}
would output:
{{ text }}
The best way is to use the templatetag tag. However, if i recall correctly, using {{ "{{text}}" }} will also work, this is however undocumented behaviour, so there is no real guarantee this will never break.

Writing eclipse templates

I am writing django templates in Eclipse->prefrences->templates, to autocomplete DJango templates. I wrote this
{% block ${cursor} %}
{% endblock %}
Now, when I request and do autocompletion, after typing {% the autocompletion is
{% {% block %}
{% endblock %}
While I would like
{% block %}
{% endblock %}
With cursor after block. How can I do this?
Instead of typing {% and selecting dj_for_empty, try typing dj_ and then auto-completing. It will behave the way you expect in that case.
BOTTOM-LINE: You auto-complete the templates into the editor based on the template name, not based on the template contents.
It appears that autocompletion has two sources: regular HTML tags (for which I can't find the definitions to change anywhere in Eclipse, sorry) and the templates themselves (which you correctly demonstrated in your comment with the screenshot).
Look at this image:
Instead of typing <t and triggering auto-complete, I typed t. You can see that there are entries with <> - indicating these are autocompletions based on the actual HTML tag - and entries with # - indicating these are autocompletions based on a template.
It appears templates are to be accessed by the name of the template. Notice that the template named table provides a complete <table> and not just the <table></table> that is autocompleted if you just type <tab and autocompletes.