Django 1.10 Print values in same line in Templates - django

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.

Related

Django: Avoid rendering as variable in template

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 %}

How do I put line break in Django template tag?

So I'm making an app that basically gets three keywords from the user. The user has to input three keywords in three separate fields, named 'kw1', 'kw2', 'kw3'. Then I've tried to render the data with template tag, like below.
*I have the template tags in script tag, because I had to put them in for loop for some reason.
...
{% for form in myKeywords %}
{
contents : "{{form.kw1}}<br>{{form.kw2}}<br>{{form.kw3}}",
}
{% endfor %}
...
I wanted to have line breaks between keywords, but in my page it shows the raw(?) text itself with the br tag in between.
What have I done wrong?
You can simply do:
{% for form in myKeywords %}
{
contents: {{ form.kw1 }}<br>{{ form.kw2 }}<br>{{ form.kw3 }},
}
{% endfor %}
rather than writing the tag in a string, because HTML doesn't recognize it's tag in a string.

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.

django display textfields

I have a form with textfields in Django, and users enter texts line by line.
When I look at the entries in admin side, I saw exactly how user wrote it but when I display it on my website, in Django templates, it just joins the sentences and I couldn't display the text line by line anymore.
To illustrate, user enters text as
-bla
-bla1
-bla2
However, it looks like in my page as
-bla1-bla2-bla3
This is my code in the template:
<div>
<p>
{{ rev.myTextField }}
</p>
</div>
In HTML, newlines between text don't imply a newline in display; you need to use HTML to insert the newline. The most straightforward way is with the <br /> tag, although there are other methods as well.
Django has a template filter for this: use |linebreaks. Here's the Documentation.
So, change:
{{ rev.myTextField }}
to:
{{ rev.myTextField|linebreaks }}
Try "safe" tag
{{ rev.myTextField|safe }}
using
{{ rev.myTextField|linebreaks }}
disables <small> tag, its better to use
{{ rev.myTextField|linebreaksbr }}
it just put <br> tag, not <p>

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.