how to render django-taggit common tags in template - django-templates

I've seen a lot of questions and answers about how to get most common tags using django-taggit. However that's not really my issue. My issue is how to pass that common tags to my django templates.
To return all tags I had:
{% for tag in model.tags.all %}
<p>{{tag}}</p>
{% endif %}
And that worked fine. But how do I return the common tags in the template? To get the common tags I have this:
class Home(ListView):
model = Model
template_name = 'home.html'
def get_common_tags(self):
common_tags = Model.tags.most_common()[:2]
return common_tags
But how do I access these common_tags? Do I need to get the common tags from the template or should I be passing them from the view?

it is better to read about Django GCBV.
in your case:
class Home(ListView):
model = Model
template_name = 'home.html'
...
def get_context_data(self, **kwargs):
kwargs['common_tags'] = self.get_common_tags()
return super().get_context_data(**kwargs)
here we override get_context_data, to get in template variable common_tags
https://docs.djangoproject.com/en/4.0/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_context_data
after that, in template:
¶
{% for tag in model.tags.all %}
<p>{{tag}}</p>
{% endif %}
<p> Most common tag are: {{ common_tags }}</p>
common_tags - is a list, you can made something with django template filter, like join:
{{ common_tags|join:" ," }}
or unordered_list:
<ul>{{ common_tags|unordered_list }}</ul>
more - here:
https://docs.djangoproject.com/en/4.0/ref/templates/builtins/#unordered-list

Related

how to define variable in phalcon php volt blade and assign data to it?

i have a variable in my blade that its set to the blade in controller
$this->view->->setVar("formData", $formData);
my formData is an array from my submitted form
now i want to define a new variable in? my volt blade and assign my formData to it.
how should i do that?
i read phalcon(https://docs.phalcon.io/3.4/en/volt) volt document but i cant find how should i do that.
In your controller setVar() and setVars() can be used on the view object to set your variables and then use them in your Volt file:
$this->view->setVar('myData', $data);
and in the template
{{ myData }}
If $data is an array and you want elements from it:
{{ myData['element1'] }}
If $data is an object you can call methods on it
{{ myData.myMethod() }}
If you want to perform comparisons and assign variables in the template:
{% if myData['element'] == 'yes' %}
{% assign reply = true %}
{% else %}
{% assign reply = false %}
{% endif %}
References:
https://docs.phalcon.io/4.0/en/volt#variables
https://docs.phalcon.io/4.0/en/volt#assignments
https://docs.phalcon.io/4.0/en/volt#if

Shopify - Change display when switching the variants (liquid)

I would like to add a new function to the product details page that shows me the current quantity of the variant that is in stock.
I get this far with Liquid, only if another variant is selected, the display of the amount does not change,
Anyone of you have any ideas how I can do that?
This shows me the current variant, but does not change when I change the selection.
{% - for variant in product.variants -%}
{{current_variant.inventory_quantity}}
{% - endfor -%}
You can't use Liquid for this. Liquid is just Shopify's templating language and is only useful for the initial page render.
I know how to do this. You need to use vue so it‘s reactive on the front end, also each product with variants now has to be a collection. You will need to loop through the products in the collection and get the linked products with javascript.
So let‘s say you have a specific product and you have three versions of it, you would actually create three separate products.
The next thing to do is link them with liquid and javascript, so in your product.liquid file open a set of script tags and you would start it like this
{% assign current_product = product %}
const product = (function() {
const product = {{ product | json }};
return product
})();
const linked_products = (function() {
const linked_products = []
{% for collection in current_product.collections %}
{% for product in collection.products %}
{% if product.id != current_product.id and product.title == current_product.title %}
product = {{ product | json }}
linked_products.push(product);
{% endif %}
{% endfor %}
{% endfor %}
return linked_products;
})();
This is just to get the ball rolling but you will need to handle quite a bit of other things and this is quite a large project.

how to parse this python code django template language

for com in applications:
for number in range(len(appliers)):
if connector[number] == com.id:
print(appliers[number].name)
I am having problem with parsing this python code to django template language... keep getting error saying that it can't parse. please can anyone parse it for me...
You can create a filter for getting a range in template:
#register.filter(name='times')
def times(number):
return range(number)
And then in template you can do:
{% for number in appliers|length|times %}
{% if connector.number == commission.id %}
{{appliers.number.name}}
{% endif %}
{% endfor %}
Alternatively you can pass range(len(appliers)) as a context variable to your template from a view.

phalcon extends volt with variable, Any other way to make that work

{% extends "../../../views/"{{variable}}"/templates/one-column.volt" %}
or
{% extends "../../../views/"~variable~"/templates/one-column.volt" %}
or any other way to make that work.
how to make it work like above. anyone can help me?
framework phalcon
If you are using variable inside the extend or include you have to omit the template extension. In your case it is .volt.
Try like this:
{% extends "../../../views/"~ variable ~"/templates/one-column" %}
Another example:
// When using variable inside the include
{% set folderVar = 'partials' %}
{% include "_layouts/"~ folderVar ~"/header" %}
// Without using variable
{% include "_layouts/partials/header.phtml" %}
Explanation of this behaviour: https://docs.phalconphp.com/en/latest/reference/volt.html#include

Django {{ c }} not rendering

Problem is that i can't get value to my template.
Views.py:
from django.shortcuts import get_object_or_404, render_to_response
from django.httpimport HttpResponse
def index(request):
c='hi'
return render_to_response('list.html', c)
list.html:
{% extends "base.html" %}
{% block content %}
list{{ c }}
{% endblock %}
It renders list but not{{ c }} what could cause this? And it gives no error..
render_to_response expects its context to be a dictionary, whereas you are passing your string directly:
def index(request):
context = { 'c': 'hi' }
return render_to_response('list.html', context)
Based on your comment below, if you want 'c' to be a list of things, it would look something like this instead:
def index(request):
context = { 'c': ['hello', 'world'] }
return render_to_response('list.html', context)
The basic idea is that you're building a mapping of the variables you want to reference in your template. The names that you reference in the template are keys in the dictionary.