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.
Related
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
I'm new with liquid and ruby, but I would like to create a custom filter in a collection, to filter by metafields. I already have:
A dropdown in the collection.liquid, with the values I would like to filter for.
When selecting a filter, it goes to a link like: https://myshop.myshopify.com/collections/my-collection/my-filter . Basically it is like the tags, but with my filter instead
However, since it is a custom filter and not a tag, I get no results. I'm wondering where is the query that displays all the products (or filters) is in the code. I know that it depends on the theme, but I'm using the default theme: launchpad-star.
Not sure if I could do it this way or with a link like: https://myshop.myshopify.com/collections/my-collection?filter_by=my-filter , in which case, I would also need where should the logic go.
I've looked at the forums already and found two closed tickets with no responses: https://ecommerce.shopify.com/c/ecommerce-design/t/using-metafields-to-create-filter-drop-downs-in-collection-liquid-187513 and https://ecommerce.shopify.com/c/ecommerce-design/t/using-metafields-to-create-filter-drop-downs-in-collection-liquid-134401 .
Thanks in advance
Probably not the best solution, but this is what I did to solve the problem:
I changed to the second option of the url, so when a user selects an option in the combobox, it is sent to a URL like: myshop.myshopify.com/collections/my-collection?filter_by=my-filter
In product-grid-item.liquid, I'm getting the metafield value of the product and displaying it as a class, and hide all the products as default. In the collection.liquid I read with javascript the value of the parameter (filter_by) and remove the "hide" class of the products with the value of the filter_by as class, so it gets displayed.
I feel that it is not very clean, but it is working as expected. Problems with this solution:
* Not displaying all the products and then filtering them
* I need to display all the products to avoid pagination, which could be a big problem if I have a lot of products.
If anyone could post a better solution, welcome!.
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>
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}}
For some reason this bit of liquid just stopped working in a site I did some months back:
{% assign packs = collections.packs.all_products %}
{{ packs[0].title }}
Accessing the array items in a for loop still works, but I am unable to access the items directly using an index value. Is this a recent change to the template system or is something else going on?
this should work again. A change in liquid that caused this was reverted last week.