Escape single curly brace in Django template - django

I am looking to generate a string like \usepackage{mypackage} from a Django template.
Suppose there is a variable package.name in the context how can I generate this?
Firstly I tried, \usepackage{{{package.name}}} but that throws TemplateSyntaxError
Then I tried \usepackage{ {{package.name}} } which works but has the two spaces in the output, ie, \usepackage{ mypackage }
Is there an easy way to generate this string with Django template engine?

You can also print strings with the mustache-syntax e.g.: {{ 'random string' }}. So we can print the corresponding curly brace like this:
\usepackage{{ '{' }}{{ package.name }}{{ '}' }}
Output:
\usepackage{mypackage}

You can do something like this using span
\usepackage{<span>{{package.name}}</span>}

one option is that you can write a template tag that will render it as you want and give the package name as a variable

Related

How to add date-time arguments to django URL tag

I am trying to get this URL in my template.
path('show_date/<int:year>/<int:month>/<int:date>/',show_date,name='show_date'),
My template
<a href="{%url 'diary:show_date'{{date|date:'Y'}} {{date|date:'m'}} {{date|date:'j'}}
%}">{{date}}</a>
returns this error
Could not parse some characters: 'diary:show_date'|{{date||date:'Y'}}
Please help me fix this issue
You shouldn't add the double braces when you use a filter on a variable in a tag, you can use the filters without them
{{ date }}

Pass in variable to tag django

is there a way to pass in a variable in a tag in django templating?
For example, Click here, as you can see, I want to pass the url_path variable into the url tag, but when I do this, django treats {{ url_path }} as the string itself, not the variable url_path. Is there a way to pass in that variable in the url tag? Thanks in advance!
You simply pass a variable without using quotes, and without using curly brackets ({{ … }}):
Click here
Don't use quotes, by using quotes Django thinks you want to pass the template page directly.

Django template tag left / right

Given text. Is there a django template tag to do:
text = 'hello there'
text[:3] ==> 'hel'
text[-3:] ==> 'ere'
Something like:
{{ text|left:3 }}
If not I can build a template tag for it but was wondering if it's already provided.
For a standard django template, you can use the slice template tag:
{{ text|slice:":3" }}
will only display the first three characters and
{{ text|slice:"-3:" }}
will only display the last three characters.
Essentially this template tag works just like the standard python slice syntax and just like standard slicing, it will work on both lists and strings. The docs only describe its use on lists so I can see why you'd be confused about this. Hope this helps!
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#slice

django template string - replace line break with line space

I have a string in my django1.4 template that I want to replace the line breaks with a whitespace. I only want to replace the line breaks with a single white space in the template string.
So far all my searces on django docs, Google and SO have not given me an answer.
Here is my string in my template:
{{ education_detail.education_details_institution_name|safe|truncatechars:20|striptags }}
When I have the following string saved:
University
Bachelor of Something
2008 - 2010
The string in the django template is rendered as:
UniversityB...
I want to replace the line break with a space between the yB like so:
University B...
How would I do this?
Here is the custom filter code that I finally got operational:
from django import template
register = template.Library()
#register.filter(name='replace_linebr')
def replace_linebr(value):
"""Replaces all values of line break from the given string with a line space."""
return value.replace("<br />", ' ')
Here is the call on the template:
{{ education_detail.education_details_institution_name|replace_linebr }}
I hope that this will help somebody else.
You can rely on built-in truncatechars filter's behavior to replace newlines with spaces. All you need is to pass a length of the string as an argument, so that you would not see your string shortened:
{% with value|length as length %}
{{ value|truncatechars:length }}
{% endwith %}
This is a bit hacky, but uses only built-in filters.
You can always write a custom filter if you need this kind of functionality to be reusable.

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