multiplication in django template without using manually created template tag - django-templates

I want to achieve multiplication operation in django template. For example
I have the values,
price=10.50
quantity=3
With the help of this link
http://slacy.com/blog/2010/07/using-djangos-widthratio-template-tag-for-multiplication-division/
i tried below codes for achieving it,
{% widthratio quantity 1 price %}
but its returning only 31. But i need the answer in float (31.5)
And i want to achieve it without using the manually created tags
How can i achieve it?
Thanks in advance...

You can use the built-in widthratio template tag.
a*b use {% widthratio a 1 b %}
a/b use {% widthratio a b 1 %}
Note: the results are rounded to an integer before returning.
#see https://docs.djangoproject.com/en/dev/ref/templates/builtins/

There are 2 approaches:
Computing the values inside the view and pass them to the template (recommended in my opinion)
Using template filters
In the manner of the add filter, you could always create your own multiply filter, creating your own custom filter:
from django import template
register = template.Library()
#register.filter
def multiply(value, arg):
return value * arg
Then in your template, something like that should work.
{{ quantity | multiply:price }}
This is not tested, and I never did this as - again - I find neater to compute datas inside the views and render only with the templates.

Another approach that I have used seems cleaner to me. If you are going through a queryset, it doesn't make sense to compute the values in your view. Instead, add the calculation as a function in your model!
Let's say your model looks like this:
Class LineItem:
product = models.ForeignKey(Product)
quantity = models.IntegerField()
price = models.DecimalField(decimal_places=2)
Simply add the following to the model:
def line_total(self):
return self.quantity * self.price
Now you can simply treat line_total as if it were a field in the record:
{{ line_item.line_total }}
This allows the line_total value to be used anywhere, either in templates or views, and ensures that it is always consistent, without taking up space in the database.

I know it's been so long since this question came out but, now there's a library called django-mathfilters which made mathematical operations easier in Django templates. you can easily write
<li>42 * 0.5 = {{ answer|mul:0.5 }}</li> for multiplication.
check it out https://pypi.org/project/django-mathfilters/

Multiplication with variable in Django.
1st: install & then register "mathfilters" at INSTALLED_APPS in setting.py
2nd: Add {% load mathfilters %} in html Page
3rd: Multiply with variable like{{response.notSatisfied|mul:100}}

Related

How can I self reference the table I'm working on in a dbt Project?

I'm looking to self-reference the table I'm working on within a model file in the config block to alias the table name. Right now, I'm naming dynamically naming the alias using a Python file for loop but would prefer if the model file recognized and designated the table name in itself.
{{ config(
alias=model.table ### this.name? not sure what syntax to use here ###
) }}
select *
from {{ source('aceso', 'aceso_accountlookup') }}
{% if is_incremental() %}
where _FIVETRAN_SYNCED > (select max(_FIVETRAN_SYNCED) from {{ this }} )
{% endif %}
Currently I have no idea the format of syntax required to get dbt to understand what I want it to do
dbt currently has a strong one-database-object-per-model association, so what you seem to be trying to describe (based on reading your answers to #tconbeer's questions in the comments) isn't really possible without something hacky like you're already doing.
There is a GitHub Discussion around making it possible for dbt to generate multiple objects from a single model here that you may wish to contribute to.

How To Enable Block Inside Fifty Different Sections Without Copying Meta Code?

I am developing a Shopify Theme. The structure I have now is (excerpt):
Snippets
custom-message-snippet
Sections
custom-message-section
welcome-page-a-section
welcome-page-b-section
Templates
welcome-page-a-template
welcome-page-b-template
...
Custom message snippet uses settings that are in the custom-message-section, that is:
a) message
b) header text
I'd like for users to be able to add custom-message-snippet to welcome-page-a and welcome-page-b in a way that settings for both are different.
I can not render section 'custom-message-section" inside welcome pages because it is not possible (and a workaround is nasty).
There are fifty welcome pages. Every welcome page is totally different.
My question is:
How to allow users to use custom-message-snippet in all welcome pages without copying and pasting custom-message-page setting schema to each and every welcome page?
combine custom-message-section to custom-message-snippet
combine welcome-page-\w+-template to welcome-page-template, you only need to have one template and schema set, the user will control the sections in schema selection.
in template, use schema to select the page-content; this, defined by a bunch of if-else /switch statement.
{% section "welcome-page-custom-message-section" %}
{% section "welcome-page-content" %}
in welcome-page-custom-message-section
{% render "custom-message-snippet" with "string from welcome-page-section schema " %}
create checkbox select schema for custom message to appear or not.
Short: You can't.
Long: Using storefront 2.0, you end you having blocks, which can be added manually to any template.
It is possible if you have a little knowledge of Node.js and you're using the Shopify CLI.
Check the solution here: Shopify Section / Block Schema In A Separate File. It is possible 

How do I add a metafield to liquid in Shopify?

I'm trying to edit a section in my Shopify theme which displays a video. I'm using the section on product pages and the URL for the video is inserted by using the theme editor tool. As such the same video inserted will show on every product page using that template. However, I would like to use a different video on each product page. I can't see a way to do this through the theme editor unless I duplicate the template for each and every product that needs its own video URL (seems a bit overkill).
I found this code which controls the video section, and the bit I'm trying to figure out is how I can change the 'assign video_id' to use a metafield so that I can add this per product by simply adding the video URL on a specific product page. I can't think of an easier way to achieve what I need, but thought changing a metafield for each product that needs a custom video URL would be the best way to do it, working possibly similar to this:
{% if template.name == 'product' %}
{% if product.metafields.my_fields.product_video_url %}
{{ product.metafields.my_fields.product_video_url }}
{% endif %}
{% endif %}
If anyone had an idea of how I can do this using the code below and inserting a metafield (and even better, a metafield IF it's filled, otherwise default back to video_url.id) I would really appreciate it.
{%- liquid
assign bg_color = section.settings.background-color
assign button_text = section.settings.button-text | escape | truncate: 30
assign button_url = section.settings.button-url | url_escape
assign full_width = section.settings.full-width
assign heading = section.settings.heading | escape
assign darken_video = section.settings.darken-video
assign light_text = section.settings.light-text
assign section_height = section.settings.section-height
assign sub_heading = section.settings.sub-heading | escape
assign thumbnail = section.settings.image
assign video_url = section.settings.video-url
if video_url.id
assign video_id = video_url.id
else
assign video_id = '_9VUPq3SxOc'
endif
assign cover_link = false
if button_text == blank and button_url != blank
assign cover_link = true
endif
assign button_type = 'button'
if light_text
assign button_type = 'inverted-secondary-button'
endif
-%}
Thank you for any help at all.
Metafields are pretty simple, and very useful. First off, you can set them in the Admin. Create one with a description.
Note the concept of my_fields is really dumb, instead, think of it as "context", the "what am I in all this". So you might use a namespace here, like "product_videos". Hence, at this point, think of it as a box named, with whatever is inside the box, still a mystery!
product.metafields.product_videos
Or your company name. Or your cat's name. Or whatever turns your crank. my_fields is just generic filler. Means nothing.
Once you get over that hump, that you've now got a namespace, you can actually get down to the nitty-gritty. Anything of value to you is likely to be a string here, a string representing a video. So you want to make the type of the Metafield to be text. A string of text.
To be of much use, you need a key. You want to grab onto the key! So if a product has a special movie, let the key guide you right to it! The key is going to be something brilliant like; video_id! So now you have a product, with a Metafield resource, in your namespace, product_videos, with a key! Namely video_id. And you can then go to that product in your Shopify Admin, and at the bottom of the details page, fill in the answer to the question of video_id. Give it some info. Where to find the video. What it is called. Anything useful to you.
Now in your theme, just reference {{ product.metafields.product_videos.video_id }} when you need it.

Get variables in Sphinx templates

I can't figure out how to get variables into Sphinx documents via a template. I am certainly making an elementary mistake, but while there is lots of documentation for using Jinja/Flask templates for web service and some documentation for Sphinx using it, I am having trouble doing the following. Maybe it's not possible and I'm barking up the wrong tree, but then this is fairly different from how variables work in general in web (as opposed to doc) templates?
I am working within a much larger project. Suppose in my project's conf.py I make a variable, say
LANGS = ['en', 'de', 'cn']
I know that this works because if I do the docbuild (we have a custom docbuild but I don't think it does anything really crazy other than a customized logger and eating a bunch of 'chatter') with the following line in conf.py
print len(LANGS)
it shows up during the docbuild.
But now of course I want to access this variable in my template. As far as I can tell, we override index.rst with templates/index.html, which inherits from the basic layout.html for Sphinx. And if I do
<p>We have {{ LANGS|len }} languages</p>
I get
We have 0 languages
Now, this is weird, because sometimes I can cause an error in the build by referring to variables not defined (though not consistently), so that somehow it 'knows' that the variable is defined but thinks it has length zero. Or does a "null" variable have length zero automatically?
How do I get this variable defined - or is it not possible?
What I want to do is then do something for each language in the list (make an outside link, in particular), but I figure there is no point in trying {% for %}/{% endfor %} or whatever if I can't get this working. Maybe Sphinx implements only a subset of Jinja?
Anyway, please help!
There are at least two ways to pass variables to a template:
Via html_context:
A dictionary of values to pass into the template engine’s context for all pages. Single values can also be put in this dictionary using the -A command-line option of sphinx-build.
Example:
# conf.py:
html_context = {'num_langs': len(LANGS)}
<!-- template: -->
<p>We have {{ num_langs }} languages</p>
Via the html_theme_options. This requires adding an option to theme.conf (you can create a theme by inheriting from a standard one):
[options]
num_langs = 1
Then you can set num_langs in conf.py via html_theme_options:
html_theme_options = {'num_langs': len(LANGS)}
and use it in a template:
<p>We have {{ theme_num_langs }} languages</p>

Django SQL query for each include/extends

I'm using Django to realize a engineering management system. I've made something really wrong somewhere, and my SQL query count is very high on some pages.
For example, I got 95 to 98 SQL queries in a single page, a simple ListView. All the queries are the same :
SELECT * FROM "syncoor_codification" LIMIT 21
They always return the same object. I suspect the queries to be triggered by my model's get_queryset() function.
If I use Django Debug Toolbar, I can see that the queries are triggered inside the template, by lines like :
{% extends 'syncoor/base.html' %}
{% extends 'syncoor/docs/base.html' %}
{% extends 'syncoor/docs/codifications/base.html' %}
{% include 'syncoor/js/jsp.js' %}
How could I get rid of this extra overhead ?
Edit : here's a screenshot :
In the picture you posted, the stack trace shows 2 things of note.
The SQL calls are being performed inside a signal handler associated with django-debug-toolbar, specifically the TEMPLATE panel.
The SQL call has it's limit clause set to REPR_OUTPUT_SIZE, Indicating that the template panel is trying to render a representation of a queryset.
This leads me to believe that you have passed a function or lazy object that returns a new queryset into the context at some point and it is this object that is being evaluated by the template panel. See if you can spot a bunch of Entity Objects in the context in the templates panel.
To confirm, try setting your settings like this.
DEBUG_TOOLBAR_PANELS = (
'debug_toolbar.panels.version.VersionDebugPanel',
'debug_toolbar.panels.timer.TimerDebugPanel',
'debug_toolbar.panels.settings_vars.SettingsVarsDebugPanel',
'debug_toolbar.panels.headers.HeaderDebugPanel',
'debug_toolbar.panels.request_vars.RequestVarsDebugPanel',
# 'debug_toolbar.panels.template.TemplateDebugPanel',
'debug_toolbar.panels.sql.SQLDebugPanel',
'debug_toolbar.panels.signals.SignalDebugPanel',
'debug_toolbar.panels.logger.LoggingPanel',
)
It looks like there are loops running in your base.html templates that are hitting the database. That's why inheriting from those templates is generating queries. Without seeing the templates themselves it's hard to say what the exact problem is, but that's certainly where you'll find the answer.