Can i have group_vars used in Jijnja2 template? - automation

I have the following variable that i want to use in Jinja2 template and i am not sure if it is even possible
tag: pony
This is how I tried using it:
{% if ansible_fqdn in groups['machines-{{ tag }}'] %}
{% for host in groups['machines-{{ tag }}'] %}
echo "Do some magic with my {{ tag }}"
{% endfor %}
{% endif %}
Is it possible ? And how of course :)
Thanks !

This works for me
{% set group = 'machine-' + tag %}
{% if ansible_fqdn in groups[group] %}
{% for host in groups[group] %}
echo "Do some magic with my {{ tag }}"
{% endfor %}
{% endif %}

Related

Shopify linking product using SEO handle

I followed the directions for the second way to tag a product to a blog
This is the website I used https://happypoints.io/shopify-add-products-to-blog-post-c2-stt-66/
This is the code that was entered
{% assign my_description = article.content | split: '=== split content ===' %}
{% assign my_description_size = my_description.size | minus: 2 %}
{{ my_description | first}}
<div class="show-product-list">
{% if article.tags.size > 0 %}
{% for tag in article.tags %}
{% paginate collections.all.products by 100 %}
{%- for product in collections.all.products -%}
{% if product.handle == tag %}
<div class="product_item">
{% include 'product-card-list' %}
</div>
{% endif %}
{%- endfor -%}
{% endpaginate %}
{% endfor %}
{% endif %}
</div>
{{ my_description | last}}
after following all the directions I received an error message saying
Liquid error (sections/article-template.liquid line 42): Could not find asset snippets/product-card-list.liquid
I am not sure why the product wont link to the blog using the seo handle
Your code has a line:
{% include 'product-card-list' %}
That means Shopify expects to find some asset named product-card-list.liquid. Since it cannot, you get that error. Add that snippet of code, and your error will disappear.

Checking if no compare_price exists in Shopify Liquid

I want to check if there's no compare price, and I cannot get any of the following to work in Shopify:
{% if price > compare_at_price %}
{% if compare_at_price == 0 %}
{% if compare_at_price == "" %}
I want to output some HTML when the compare_price doesn't exist.
You are missing the object here to get its attributes:
{% if product.price > product.compare_at_price %}
Do something
{% endif %}
To check if there is one:
{% if product.compare_at_price %}
Do sthg
{% endif %}
To check if there isn't one:
{% unless product.compare_at_price %}
Do sthg
{% endunless %}
Documentation:
https://shopify.dev/docs/themes/liquid/reference/objects/product

TemplateSyntaxError at /admin/ Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag?

**
TemplateSyntaxError at /admin/
Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag?
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 2.1
Exception Type: TemplateSyntaxError
Exception Value:
Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag?
Exception Location: C:\Users\subramanyam.s.g\PycharmProject\pyyshop\LEARN\lib\site-packages\django\template\base.py, line 522, in invalid_block_tag
Python Executable: C:\Users\subramanyam.s.g\PycharmProject\pyyshop\LEARN\Scripts\python.exe
Python Version: 3.8.3**
I also happened to come across this error while using Django Version: 2.1.
I was customizing the admin look and feel as per the django documentation 3.1 https://docs.djangoproject.com/en/3.1/intro/tutorial07/.
Django Version: 2.1 can be hosted in a shared hosting domain with reference to https://pythonfusion.com/deploy-django-on-shared-hosting/.
Here is part of the screenshot to just show you i got the same error and see where the problem https://i.stack.imgur.com/RV52y.png
If you have difficulty finding where the Django source files(the ones you want to customise eg admin) are located on your system, use the following procedure:
In your computer locate where your django project is located in the file explorer e.g
C:\Users\yourname\Desktop\yourproject.
In your file explorer window, there is a path above your folder showing your current
location in the file explorer e.g This PC > Desktop > yourproject. Click inside this
path and rewrite cmd and then hit enter key on your keyboard. A command prompt will be
opened.
Now if you installed django in a global environment, that is, in local disk without
creating a virtual environment enter this code python -c "import django; print(django.__path__)" in the opened command prompt.
If you installed Django into the virtual environment then activate first the virtual environment using still the opened Command prompt, i normally use workon myvirtualenvironmentname to activate my virtual environment. Then paste this code python -c "import django; print(django.__path__)" in the opened command prompt.
Hit enter key on your keyboard and you will see something like this ['C:\\Users\\yourname\\Envs\\yourvirtualenvironmentname\\lib\\site-packages\\django']. This is the location we should arrive to access the Django resources.
Go to your file explorer by pressing CTRL + E on your keyboard.
Navigate to your local disk C, open the Users folder, next open the folder with your name next locate and open the Envs folder and locate your yourvirtualenvironmentname your project is using.
Open your yourvirtualenvironmentname> Lib > site-packages > django > contrib > admin > templates >admin.
You should be here C:\Users\yourname\Envs\yourvirtualenvironmentname\Lib\site-packages\django\contrib\admin\templates\admin in your machine.
Now take key note on the Django version you are using. Where we are currently there are some files that are in Django Version: 2.1 and not in Django Version: 3.1 e.g app_list.html is in Django Version 2.1 and not in 3.1 with respect to this directory where we are.
Note 2. The file names maybe the same but the content inside the file may differ slightly and this is what causes the error
The code below is some of the content in base.html (i have just copied a few of the code where i want to emphasize ) from Django V 3.1.
base.html from Django Version 3.1
`
{% if not is_popup %}
<!-- Header -->
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% block usertools %}
{% if has_permission %}
<div id="user-tools">
{% block welcome-msg %}
{% translate 'Welcome,' %}
<strong>{% firstof user.get_short_name user.get_username %}</strong>.
{% endblock %}
{% block userlinks %}
{% if site_url %}
{% translate 'View site' %} /
{% endif %}
{% if user.is_active and user.is_staff %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
{% translate 'Documentation' %} /
{% endif %}
{% endif %}
{% if user.has_usable_password %}
{% translate 'Change password' %} /
{% endif %}
{% translate 'Log out' %}
{% endblock %}
</div>
{% endif %}
{% endblock %}
{% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% block breadcrumbs %}
<div class="breadcrumbs">
{% translate 'Home' %}
{% if title %} › {{ title }}{% endif %}
</div>
{% endblock %}
{% endif %}
<div class="main shifted" id="main">
{% if not is_popup and is_nav_sidebar_enabled %}
{% block nav-sidebar %}
{% include "admin/nav_sidebar.html" %}
{% endblock %}
{% endif %}
<div class="content">
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear">
</div>
<!-- END Content -->
{% block footer %}<div id="footer"></div>{% endblock %}
</div>
</div>
`
I wanted to highlight these lines of code but i was unable. So check of the following in your base.html {% translate 'Welcome,' %}, {% translate 'View site' %}, {% translate 'Documentation' %}, {% translate 'Change password' %}, {% translate 'Log out' %} and {% translate 'Home' %}.
Lets compare the above base.html file with that of Django Version 2.1. Take a close look to discover the difference.
base.html file in Django Version 3.1
`
{% if not is_popup %}
<!-- Header -->
<div id="header">
<div id="branding">
{% block branding %}{% endblock %}
</div>
{% block usertools %}
{% if has_permission %}
<div id="user-tools">
{% block welcome-msg %}
{% trans 'Welcome,' %}
<strong>{% firstof user.get_short_name user.get_username %}</strong>.
{% endblock %}
{% block userlinks %}
{% if site_url %}
{% trans 'View site' %} /
{% endif %}
{% if user.is_active and user.is_staff %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
{% trans 'Documentation' %} /
{% endif %}
{% endif %}
{% if user.has_usable_password %}
{% trans 'Change password' %} /
{% endif %}
{% trans 'Log out' %}
{% endblock %}
</div>
{% endif %}
{% endblock %}
{% block nav-global %}{% endblock %}
</div>
<!-- END Header -->
{% block breadcrumbs %}
<div class="breadcrumbs">
{% trans 'Home' %}
{% if title %} › {{ title }}{% endif %}
</div>
{% endblock %}
{% endif %}
{% block messages %}
{% if messages %}
<ul class="messagelist">{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message|capfirst }}</li>
{% endfor %}</ul>
{% endif %}
{% endblock messages %}
<!-- Content -->
<div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content %}
{% block object-tools %}{% endblock %}
{{ content }}
{% endblock %}
{% block sidebar %}{% endblock %}
<br class="clear">
</div>
<!-- END Content -->
{% block footer %}<div id="footer"></div>{% endblock %}
`
You will see the following in your base.html {% trans 'Welcome,' %}, {% trans 'View site' %}, {% trans 'Documentation' %}, {% trans 'Change password' %}, {% trans 'Log out' %} and {% trans 'Home' %}.
To solve your *TemplateSyntaxError at /admin/ Invalid block tag on line 23: 'translate', expected 'endblock'. Did you forget to register or load this tag?
*
Just rename the translate to trans and save your file. In conclusion whenever you will add app_list.html or any other file in your admin folder and get and error. Just open the file and rename anything that has translate to trans. This solved my problem.
Incase i miss quoted something for example the django versions remember to let me know. Lets learn together. Thank you
Only way I can come up with for this could happen is if you've created a custom tag module named 'i18n' which would override the builtin i18n module. I can reproduce it by creating a file 'i18n.py' in my app's templatetags/ folder - is that what you have done?
In my case it was a template I've copied and modified. The template was from newer version of django. After upgrading django it all normalised.

shopify pass a variable to settings

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}

Django simple-friends templte help!

I have been trying to do this forever it seems.
I don't understand what to put in the templates and can find no guidelines help.
My Profile page
need friends list
something like:
{% for friends in Friendship.objects%}
<li>are_friends: {{ friends.are_friends.user.username}}</li>
<li>is_invited: {{ friends.is_invited}}</li>
{% endfor %}
Well being a bit of newcomer to Django and programming in general this took me ages to work out but I got there in the end so Ill share what I did. First of all though the code might no be exactly what you need but you will get the idea. I also might have made some mistakes here or there is better way to do it so don't assume this is the most correct way but it does work.
First of all I split my code into two templates. One for when I am on the profile page of another user and one for when I am on my "Manage my Friends" page. Please bare in mind this is basically pure template code and there has been no stylings applied. Thats up to you to do.
{% if user|friends %}
{% with user|friends as list %}
Friends List
{% for m in list %}
{{ m }}
{% endfor %}
{% endwith %}
{% else %}
Search for Friends
{% endif %}
{% if user|friendshiprequests %}
{% with user|friendshiprequests as list %}
{% if list.received %}
Friendship Requests Received
{% for m in list.received %}
{{ m }}
Accept Request
Decline Request
Block User
{% endfor %}
{% endif %}
{% if list.sent %}
Pending Friend Requests Sent by You
{% for m in list.sent %}
{% if not user|isblockedby:m.to_user %}
{{ m.to_user }}
Cancel Request
{% endif %}
{% endfor %}
{% endif %}
{% endwith %}
{% endif %}
{% if user|blocks %}
{% with user|blocks as list %}
{% if list.applied %}
List of Blocked Users
{% for m in list.applied %}
{{ m }}
{% endfor %}
{% endif %}
{% endwith %}
{% endif %}
For the profile page of another user I have the following code. Bear i nmind I have a profile application which is passing in the username of the profile I am looking at and want to interact with. You may need to find another way to do it.
{% if not user == profile.user %}
{% if not user|isblockedby:profile.user %}
{% if not profile.user|isblockedby:user %}
{% if not user|isfriendswith:profile.user %}
{% if not profile.user|isfriendshiprequest:user %}
{% if user|isfriendshiprequest:profile.user %}
You have already sent a friend request
Cancel Friend Request
{% else %}
Send Friend Request
Block User
{% endif %}
{% else %}
{% endif %}
{% else %}
You and {{ profile.user}} are friends
trans Unfriend
Block User
{% endif %}
{% else %}
You have blocked this User
Unblock User
{% endif %}
{% else %}
You have been blocked by this user
{% endif %}
{% endif %}
{% if not user == profile.user %}
{% if not user|isblockedby:profile.user %}
{% if not profile.user|isblockedby:user %}
{% if not user|isfriendswith:profile.user %}
{% if profile.user|isfriendshiprequest:user %}
Accept Friendship Request
Accept Request
Decline Request
Block User
{% endif %}
{% endif %}
{% endif %}
{% endif %}
{% endif %}
Hope this helps.
This guide should be a good start:
http://docs.djangoproject.com/en/dev/ref/templates/
Let's analyze the code a little bit:
Friendship.objects would return the default manager on Friendship model. At this point you probably want to call one of the methods that return an iterator on this manager. For example the following template code iterates over all Friendship's:
{% for friendship in Friendship.objects.all %}
But this doesn't make a lot of sense. You probably want to iterate over Friendships for a particular user. The following code iterates over friends of the active user:
{% for friendship in user.friendship.friends.all %}
{{ friendship.user.username }}
{% endfor %}
The code above may not work. I can't remember why. But there is already a tag that gives you friends of a specific user:
{% load friends_tags %}
{% friends_of user %}
{% for friend in friends %}
{{ friend.username }}
{% endfor %}
Rant Section
It seems the app is not usable without reading the source. This is not good at all. The author of this app should have written better documentation. I should allocate some time to work on these issues sometime.