Pass in variable to tag django - 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.

Related

How can I scape a double qoutes inside a django tag?

I would like to escape the these quotes "d-m-Y" in the code below. How can I escape these quotes, they make my project crash.
<td>{{ tenderEnquiries.c }} tenders</td>
Thanking you in advance.
Daniel has the correct cause, but the solution is much simpler; just remove the inner template tag completely, along with the quotes.
{% url 'tender_list' date=tenderEnquiries.d_assigned|date:"d-m-Y" %}
The quotes are not the problem. The problem is that you try to nest a template tag inside a template tag, which the Django template language does not support.
You have several options:
pass the equivalent of tenderEnquiries.d_assigned|date:"d-m-Y" as a template variable and use it to reverse the URL
reverse the URL in your view and pass it as a template variable
create a custom template tag that receives tenderEnquiries as a parameter and renders the URL

Django template 'url' filter encodes a substitution string

Using django1.9
my urls would look something like example.com/my_url/cachebuster
in my email.html template I have something like
{% url my_url cachebuster %}
Now cachebuster is actually "%%cachebuster%%"
This is a substitution string which my email api is parsing for, and whenever it finds "%%cachebuster%%" it replaces it with the specific users cachebuster.
My issue is that the django {% url %} function automatically encodes that string to "%25%25cachebuster%25%25"
Which means the api never finds "%%cachebuster%%" and never replaces it
So instead of a dynamic url, its always example.com/my_url/%25%25cachebuster%25%25
How can I fix this, without changing the substitution string? I've tried using mark_safe but haven't got it to work
The only way I can think of is to hardcode /my_url/{{cachebuster}} without using the django url tag, but this is bad practice because the url templatetag was created for a reason

What is the proper syntax for using a django template variable as a jade href id reference?

I am trying to generate a navigation table using a django template for loop. The href target includes a django variable. The django template is written in jade format.
If i were using HTML format in the django template, I would use:
{{group.name}}
But I can't find the correct syntax using jade format.
What I have is:
table.generic
tbody
tr
th Jump to a Group
{% for group in groups %}
tr
td
a(href!='#{{group.name}}') {{group.name}}
{% endfor %}
But the anchors are rendered in HTML as:
<a +{group.name.__str__()+'}'="" href="">Registration</a>
"Registration" is a value for group.name.
Removing the ! causes a django render error.
I tried to insert a backslash: a(href!='#\{{group.name}}') {{group.name}}, but this renders with the backslash intact as:
Registration
Suggestions?
Edit: As a terrible work-around, I have added a "pound" method to return the group name with a # prefix:
def pound(self):
return unicode("#" + self.name)
Now I can reference {{group.pound}} in the jade template:
a(href!='{{group.pound}}') {{group.name}}
Maybe a better method name would be group.name_as_id_tag ....
RESOLVED: Format the django template variable as a jade variable
Based on the last suggestion from #user1737909, I extended the suggestion to try:
a(href='##{group.name}') {{group.name}}
The key is to format the double-curly django variable {{}} as pound-single-curly jade variable #{}. Do not include the ! before the = sign so the variable is evaluated by jade which will insert the value it gets from django.
I wish I could find a link to a coding style reference page to include here. But if I had one, I would not have asked this question in the first place.
Based on the last suggestion from #user1737909, I extended the suggestion to try:
a(href='##{group.name}') {{group.name}}
The key is to format the double-curly django variable {{}} as pound-single-curly jade variable #{}. Do not include the ! before the = sign so the variable is evaluated by jade which will insert the value it gets from django.
OP updated to reflect this solution.

Django adds %2f in place of the / in the template for an image field

I am having a few issues with a django template with a mimetype="text/plain".
Firstly the s3 part of url is rendering with the :80 on the end and then the actual image url is rendering with '%2f' in replace of each slash.
object.image.url
I have tried safe and other custom tags to replace the '%2f' and it just wont work
#what I have
http://blahblah.s3.amazonaws.com:80/navigation%2Fprimary%2Fimage.jpg
#what I want
http://blahblah.s3.amazonaws.com/navigation/primary/image.jpg
The custom tag I have tried along side safe is:
import re
from django import template
register = template.Library()
def reslash (value):
return value.replace('%2f', '/')
register.filter('reslash', reslash)
used like this:
{{ object.image.url|reslash }}
But it doesn't work. Thanks
Django automatically html escapes all variables in templates. In order to insert the value of a variable without escaping you should use the safe filter to tell django the value does not need to be auto-escaped like so:
{{ object.image.url|safe }}

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.