django-sorting: Localization is not working - django

I am using django soring application:
https://github.com/directeur/django-sorting
I just wonder if there is a way to make local names for sorting filters...
E.g. I am trying to localize following:
<th>{% anchor total Rating %}</th>
And using standard django trick
<th>{% anchor total _("Rating") %}</th>
is not helping... Don't know what to do...

you should use the trans template tag from within templates..
https://docs.djangoproject.com/en/dev/topics/i18n/internationalization/#trans-template-tag
UPDATE
If you want the title to be translated, you'll just need to modify the anchor template tag code of django-sorting, for example, looking at the source here:
https://github.com/directeur/django-sorting/blob/master/django_sorting/templatetags/sorting_tags.py
Inside anchor you could modify it, for example, by adding the ugettext function as "_()" when the title is passed to the SortAnchorNode class:
return SortAnchorNode(bits[1].strip(), _(title.strip()))
Or you can choose another place to fire the translation, this is just for demonstration but it should work
that will translate the title you specify in your tag:
{% anchor total "Result" %} //{% anchor field title %}
You need to be sure to have the words you'll pass translated in your dictionaries..

Related

Tag or inherit the same code in Django template with minor changes

I have a bunch of code that I will need to use repeatedly on a page, and on multiple pages. For example, here is a shorter version of the code:
<a href="#"
data-toggle="popover"
title="{% for terms in s_terms %}{% if terms.slug == 'neuron' %}{{terms.title}}{% endif %}{% endfor %}"
data-content="{% for terms in s_terms %}{% if terms.slug == 'neuron' %}{{terms.para_one}}{% endif %}{% endfor %}">
Toggle popover
</a>
There is a lot more code in the block. Now, for obvious reasons I do not want to keep repeating such large chunks of code. I am a fan of the DRY approach.
However, I can't figure out how to render this same piece of code repeatedly. The only thing that would change is the word = "neuron" in there. I thought of using template tags, but that didn't work.
I tried saving the code as a separate file, and inherit it within my template, but then I can't change the keyword ('neuron'). I also tried creating a separate dynamic page, and include that in my Django template, but looks like the include tag only works for templates, and not for dynamic pages.
Can anyone help, please? Thank you, in advance.
You could use Django template built-in template tag include.
From the documentation:
Loads a template and renders it with the current context. This is a
way of “including” other templates within a template.
So, you can just extract your snippet in a separate template and then use it with:
{% include "snippet_template.html" %}
Additionally, you can pass a variable to the include template using the with keyword - you would use this to pass your word parameter:
{% include "snippet_template.html" with word="neuron" %}
As #bonidjukic wrote the include statement is what you search.
But include statement inside for-loop could reach one weakness of Django template Engine (vs Jinja). You include just variables, so it will be fast.
In the case of needing tags (like trans), Django will load tags at each include. Where Jinja will have global "tags".
So just be careful, with how you DRY you templates.

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.

Django conditional template inheritance

I have template that displays object elements with hyperlinks to other parts of my site. I have another function that displays past versions of the same object. In this display, I don't want the hyperlinks.
I'm under the assumption that I can't dynamically switch off the hyperlinks, so I've included both versions in the same template. I use an if statement to either display the hyperlinked version or the plain text version. I prefer to keep them in the same template because if I need to change the format of one, it will be easy to apply it to the other right there.
The template extends framework.html. Framework has a breadcrumb system and it extends base.html. Base has a simple top menu system.
So here's my dilemma. When viewing the standard hyperlink data, I want to see the top menu and the breadcrumbs. But when viewing the past version plain text data, I only want the data, no menu, no breadcrumbs. I'm unsure if this is possible given my current design. I tried having framework inherit the primary template so that I could choose to call either framework (and display the breadcrumbs), or the template itself, thus skipping the breadcrumbs, but I want framework.html available for other templates as well. If framework.html extends a specific template, I lose the ability to display it in other templates.
I tried writing an if statement that would display a the top_menu block and the nav_menu block from base.html and framework.html respectively. This would overwrite their blocks and allow me to turn off those elements conditional on the if. Unfortunately, it doesn't appear to be conditional; if the block elements are in the template at all, surrounded by an if or not, I lose the menus.
I thought about using {% include %} to pick up the breadcrumbs and a split out top menu. In that case though, I'll have to include it all the time. No more inheritance. Is this the best option given my requirement?
You can put your hyperlinks inside a block that is overridden by the loading templates.
Let's say you have your framework.html like this:
{% extends "base.html" %}
<html>...<body>...
{% block hyperlinks %}
your hyperlinks here
{% endblock %}
rest of the code
</body></html>
You can then create something of a nolinks.html template and use it
{% extends "framework.html" %}
{# here you'll have everything from framework
but now we disable the breadcrumbs #}
{% block hyperlinks %}{% endblock %}
If you're getting the past data you can then use nolinks to render instead of framework.
I hope this helps.
From here: Any way to make {% extends '...' %} conditional? - Django
It can be done like this :
{% extends ajax|yesno:"ajax_base.html,main_base.html" %}
Or:
{% extends a_variable_containing_base_template_name %}
Which ever best suited for you.
Regards;

Is is possible to html encode output in AppEngine templates?

So, I'm passing an object with a "content" property that contains html.
<div>{{ myobject.content }}</div>
I want to be able to output the content so that the characters are rendered as the html characters.
The contents of "conent" might be: <p>Hello</p>
I want this to be sent to the browser as: &amplt;p&ampgt;Hello&amplt;/p>
Is there something I can put in my template to do this automatically?
Yes, {{ myobject.content | escape }} should help (assuming you mean Django templates -- there's no specific "App Engine" templating system, GAE apps often use the Django templating system); you may need to repeat the | escape part if you want two levels of escaping (as appears to be the case in some but not all of the example you supply).
This is Django's django.utils.html.escape function:
def escape(html):
"""Returns the given HTML with ampersands, quotes and carets encoded."""
return mark_safe(force_unicode(html).replace('&', '&').replace('<', '&l
t;').replace('>', '>').replace('"', '"').replace("'", '''))
Also, see here.

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