I am caching in my django template like this.
{% load cache %}
{% cache cache_timeout key_name model_id %}
<div>
{{some_variable}}
</div>
{% endcache %}
Where cache_timeout is a variable I set in the view with the timeout.
I am also manually changing the cache value using the snippet on the accepted answer to this question
It is working fine as I know the template sets the value with the same key I use. The thing is that when I update the cache (I am checking the updated value and it is the one I set) the template keeps showing the previous value for some time(way smaller than the timeout) before updating.
I need the template to update the value read immediately.
Check the HTTP headers that are coming back with the response. Sounds like browser caching.
Related
The purpose of this control flow block is checking to see if the existing level attribute is blank or an empty string, if it is, I would like to assign a default Level 1 to the code block. If the level attribute exists and is not blank, I would like to grab the current value of the customer's level. Somehow the Liquid engine is throwing an error in my code.
Can someone pinpoint where I've done wrong in my code? Thanks so much!
{% if customer.level == blank or customer.level =='' %}
{% capture customer.level %}Level 1{% endcapture %}
Hey! Your rewards level is: {{ customer.level }}
{% else %}
Customer level is: {{ customer.level }}
{% endif %}
In Shopify's Liquid, objects cannot be changed. Your attempt to assign a value to customer.level is failing because you are not allowed to overwrite, delete or add values in something like the customer object. (The . between customer and level indicates that "level" is a property of the "customer" object - customer.level does not represent a single variable on its own)
To fix the code, all you need to do is come up with a variable name that does not use the . character (or any other punctuation that can be interpreted by the code parser). For example, naming your variable customer_level (using an underscore) should do the trick for you.
Also note that level is not a property of a customer object in Shopfiy, and you cannot add arbitrary fields to the core object types. I would recommend storing your level as either a tag or a metafield on the customer if you aren't already. Note that assigning an appropriate tag or metafield cannot be done through the Liquid code, it has to be added by some app or process that has appropriate permissions.
I'm building a django template to duplicate images based on an argument passed from the view; the template then uses Jinja2 in a for loop to duplicate the image.
BUT, I can only get this to work by passing a list I make in the view. If I try to use the jinja range, I get an error ("Could not parse the remainder: ...").
Reading this link, I swear I'm using the right syntax.
template
{% for i in range(variable) %}
<img src=...>
{% endfor %}
I checked the variable I was passing in; it's type int. Heck, I even tried to get rid of the variable (for testing) and tried using a hard-coded number:
{% for i in range(5) %}
<img src=...>
{% endfor %}
I get the following error:
Could not parse the remainder: '(5)' from 'range(5)'
If I pass to the template a list in the arguments dictionary (and use the list in place of the range statement), it works; the image is repeated however many times I want.
What am I missing? The docs on Jinja (for loop and range) and the previous link all tell me that this should work with range and a variable.
Soooo.... based on Franndy's comment that this isn't automatically supported by Django, and following their link, which leads to this link, I found how to write your own filter.
Inside views.py:
from django.template.defaulttags import register
#register.filter
def get_range(value):
return range(value)
Then, inside template:
{% for i in variable|get_range %}
<img src=...>
{% endfor %}
I'm trying to insert a metafield variable within an asset url as below:
{% assign review = product.metafields.review %}
{% assign key = 'rating' %}
<img src="{{ '[review.rating].png' | asset_url }}"/>
For some reason it isn't returning the actual variable, instead the text itself, is there a way to go about doing this?
If any reviews actually exist at the namespace product.metafields.review then you have to iterate through them. When you do that, for each iteration you'll get some key value pairs. With those you can print out the actual data of the metafield resources. What you are attempting there in your snippet seems a bit off. Try accessing the rating key in your iterator, and if it exists, the value would be available to you for your image snippet.
How can I make the generated HTML be cleaner in terms of whitespce?
Django Templating seems to be very sloppy about it.
For example, tags it recognizes, like IFs or FORs are parsed then replaced by an empty line.
Another example is when I include a file with N linkes of HTML code. If the include statement is tabbed, the first linke from the included file is indented propertly, the rest are pulled to the left.
And so on.
{% spaceless %} doesn't seem to do anything.
Is there a setting somewhere about how whitespace should be treated?
Or another solution?
Thank you.
I found this while looking for the answer to the same question and it seems like there isn't a clean and clear way to do this using the Django syntax (that I have found but I may have overlooked something) so on that note I'd recommend Jinja2. I have experience with using it for whitespace removal with SaltStack. One method is change {% this %} to {% this -%} which causes no newline to be appended so if you have a line containing only {% this -%} then it won't appear as anything in the generated html.
You can override the NodeList's render method as I've done. See my question with working code (applies only to the block and include tags):
Proper indentation in Django templates (without monkey-patching)?
There is a ticket in Django's issue tracker about having better handling of whitespace. It was closed as wontfix in 2014, with the following comment:
As far as I know, the consensus among the core team is that:
the Django template language is good enough for generating HTML, which isn't sensitive to whitespace;
the long term plan is to replace it with a better engine (most likely Jinja), not to keep working on it;
if you have more specialized needs (want to generate RTF?) just use another template engine, there are several to choose from.
For HTML output, I'm personally fine with the messy whitespace. It will have minimal effect on HTTP response sizes, if the responses are compressed.
I've had to work on a few cases where precise whitespace and newline control is important (for example, I have a template for Telegram messages, each newline will be a line break in the final message). To tidy up the template, I ended up writing a couple custom tags: {% linemode %} and {% line %}.
Example template:
{% linemode %}
{% line %}Line one.{% endline %}
This content will be ignored.
{% if True %}
{% line %}Line two.{% endline %}
{% endif %}
{% endlinemode %}
Result:
Line one.
Line two.
The idea here is that, the {% linemode %} block will throw away everything that is not also wrapped in {% line %} tags. That way, the {% if ... %} and other bits don't add unwanted spaces or newlines. Source here.
I'm trying to get Ember & Handlebars to work with Django 1.4.2 without much success. I'm using Django-ember (latest version from here): https://github.com/noirbizarre/django-ember, Handlebars.js (1.0.rc.1) & Ember.js (1.0.0-pre.2).
Following the Django-ember instructions, I've added 'ember' to the installed apps in settings, placed {% load ember %} at the top of my template, and then placed this in a block:
{% handlebars "application" %}
{{App.name}}
{% endhandlebars %}
where I've declared App with name variable in a js file.
The output is just blank however, no script tag is inserted as viewed in the browser console window (Chrome).
If I completely remove the javascript includes, the script tag is rendered - just with "{{App.name}}" left entact as expected. So it looks like, Ember/Handlebars isn't rendering the template correctly. Any ideas why?
A probably related quirk is that if I try
{% tplhandlebars "tpl-infos" %}
{{total}} {% trans "result(s)." %}
<p>{% trans "Min" %}: {{min}}</p>
<p>{% trans "Max" %}: {{max}}</p>
{% endtplhandlebars %}
it throws a template error about {% trans being an invalid tag. This can be fixed by including {% load i18n %} but seeing as this isn't in the Django-Ember instructions, I'm inclined to think this is a sign of a bigger problem; perhaps it's not designed to work with Django 1.4.2.
Update
Seems it was actually rendering after all but that it's wrapping it in a div and putting it right at the bottom of the page where I hadn't noticed it. So, now the next question is how do I get this back at it's orignal point in the html?