How do i dynamically/evaluate via expression json property in django template - django-templates

reading in a JSON for sport. using a partial for matchup markup.
awayteam and home team for most part share identical markup but the JSON properties which I have no control are such below:
<div class="away {{game.awayTeam_last_name|slugify}}">
<a href="#" title="{{game.awayTeam_first_name}} {{game.awayTeam_last_name}}">
<span class="{{league}}-logo"></span>
<span class="city">{{game.awayTeam_first_name}}</span>
{% if game.event_status != "pre-event" %}
<span title="score">{{game.awayTeam_score}}</span>
{% else %}
<span title="record entering game">(0-0)</span>
{% endif %}
</a>
</div>
<span>#</span>
<div class="home {{game.homeTeam_last_name|slugify}}">
<a href="#" title="{{game.homeTeam_first_name}} {{game.homeTeam_last_name}}">
<span class="{{league}}-logo"></span>
<span class="city">{{game.homeTeam_first_name}}</span>
{% if game.event_status != "pre-event" %}
<span title="score">{{game.homeTeam_score}}</span>
{% else %}
<span title="record entering game">(0-0)</span>
{% endif %}
</a>
</div>
is there a way to shrink/refactor the above like some expression valuator to make home and away passed via a variable.

didn't answer own question but i did get Data guys to better format the data, so became..
awayteam: { first_name:'', last_name:'', score:'' }
hometeam: { first_name:'', last_name:'', score:'' }
allowing me to shrink in half the template =)

Related

How to add product metafeilds on any page in shopify

I am attempting to add a product meta fields in a quick view product option on my product collection,
Here is the code I am using, the meta field in the tag is not working
{%- if collection.title == "vluevlockers" -%}
<div class="sizing-text">
<h2 class="help-text">sizing information of the frames</h2>
<p>{{ product.metafields.custom.frames_set_blueblocker }}</p>
<div class="bold_options"> </div>
</div>
{%- endif -%}
When I try to console.log the meta field it gives me null, But the same meta field is working fine on the single product page.

'size' on custom product metafield returns '0'

I'm building a component which checks the character count of a custom product metafield; if it's above 24 characters, the data is output as an accordion, otherwise the metafield content is printed in its entirety. The conditional below fails as the metafield size always returns 0, but I can see the content printing via the else statement so I'm certain the path is valid:
{% if product.metafields.custom.product_note.size >= 24 %}
<div class="product-note has--dropdown">
<span class="item__heading item__trigger">Product information</span>
<div class="item__content">
{{ product.metafields.custom.product_note }}
</div>
</div>
{% else %}
<div class="product-note">
<div class="item__content">
{{ product.metafields.custom.product_note }}
</div>
</div>
{% endif %}
I'm not sure it's relevant, but the product_note metafield is a multi-line text field. If anyone could point me in the right direction as to why size is failing to produce a value, I'd appreciate it massively.
You could always try working off the value contained in the metafield. It appears you are short-cutting by referring to just the combo of namespace and key, without actually saying: what is the length of the value stored at that namespace and key. Just a thought. You could at least try that.
Final answer, courtesy of #David Lazar's suggestion:
{% assign data_product_note = product.metafields.custom.product_note.value %}
{% if data_product_note.size >= 24 %}
<div class="product-note has--dropdown">
<span class="item__heading item__trigger">Product information</span>
<div class="item__content">
{{ data_product_note }}
</div>
</div>
{% else %}
<div class="product-note">
<div class="item__content">
{{ data_product_note }}
</div>
</div>
{% endif %}

Can not display my comment lists for a post

i have tried to add and display comments list in my django post_detail template yet its not displaying...
This is my comment models...
class Comment(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)
content = models.TextField()
active = models.BooleanField(default=True)
created = models.DateTimeField(blank=True, auto_now=False, auto_now_add=True)
class Meta:
ordering = ['created']
def __str__(self):
return "Comment by {} on {}".format(self.author, self.content)
i then added the comment model into my post module as follows...
from comments.models import Comment
class Post(models.Model):
comment = models.ForeignKey(Comment, blank=True, null=True)
Here is my post views.py
def post_detail(request, slug):
post = get_object_or_404(Post, slug=slug)
return render(request, "blog/post_detail.html", {
'post':post,
})
comments = post.comment.filter(active=True)
if request.method == 'POST':
#A comment was posted
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid() and user.is_authenticated():
#Create new comment but dont save to DataBase first
new_comment = comment_form.save(commit = False)
#Assigns the current instance to the comment
new_comment.post = post
#Saves the comment to DataBase
new_comment.save()
else:
comment_form = CommentForm()
return render(request, "blog/post_detail.html", {
'post':post,
'comments': comments,
'comment_form': comment_form
})
finally i tried to display these into my template yet i couldn't get the list of comments for each post
<!-- displays available comments for this post -->
{% for comment in comments %}
<div class="media d-block d-md-flex">
<img class="d-flex rounded-circle avatar z-depth-1-half mb-3 mx-auto" src="https://mdbootstrap.com/img/Others/documentation/img (2)-mini.jpg" alt="Avatar">
<div class="media-body text-center text-md-left ml-md-3 ml-0">
<h5 class="mt-0 font-weight-bold blue-text">comment {{ forloop.counter }} by {{ comment.author }} {{ comment.created }}</h5>
{{ comment.content|linebreaks }}
</div>
</div>
{% endfor %}
</div>
</div>
<!--/.Comments-->
{% endif %}
<!-- Displays comment form for registered users else link visitors to registeration page -->
{% if user.is_authenticated %}
{% if new_comment %}
<h4><span class="badge badge-pill green">your comment has been added<i class="fa fa-check" aria-hidden="true"></i></span></h4>
{% else %}
<form method="POST">
{% csrf_token %}
<div class="form-group shadow-textarea">
<label for="exampleFormControlTextarea6">Add a comment</label>
<textarea class="form-control z-depth-1" id="exampleFormControlTextarea6" rows="3" placeholder="Write something here...">
{{ comment_form.as_p }}
</textarea>
</div>
<div class="text-center mt-4">
<button class="btn btn-info btn-md" type="submit" value="add comment">submit</button>
</div>
</form>
{% endif %}
{% else %}
<h4>You have to register in order to comment this post </h4>
{% endif %}
please i need help getting this to display
it have eaten up my day.
I finally found the error...
i was rendering the post_detail template before the comment_form
here is my final code
def post_detail(request, slug):
post = get_object_or_404(Post, slug=slug)
comments = Comment.object.filter(active=True)
if request.method == 'POST':
#A comment was posted
comment_form = CommentForm(request.POST or None)
if comment_form.is_valid() and user.is_authenticated():
#Create new comment but dont save to DataBase first
new_comment = comment_form.save(commit = False)
#Assigns the current instance to the comment
new_comment.post = post
#Saves the comment to DataBase
new_comment.save()
else:
comment_form = CommentForm()
return render(request, "blog/post_detail.html", {
'post':post,
'comments': comments,
'comment_form': comment_form
})

Shopify: issue with url image in linklists

i'm pretty new to Shopify and I'm facing a strange issue in linklists.
Yesterday I changed the featured image for a category I displayed into a linklist, but I cannot see the changes in the page that prints the linklist.
I analyze the .liquid file that prints the linklists and I found the snippet that produces the divs:
{% for link in linklists[linklist].links cols: 4 %}
<div class="products item {{ link.handle }}">
<a href="{{ link.url }}" title="Browse our {{ link.object.title | escape }} collection.">
<img src="{{ link.object.image.src | collection_img_url: 'large' }}" alt="{{ link.object.title | escape }}" />
<big>{{ link.title }}</big>
</a>
</div>
{% endfor %}
After some shots I tried to add a data attribute to the image to print again the link.object.title:
{% for link in linklists[linklist].links cols: 4 %}
<div class="products item {{ link.handle }}">
<a href="{{ link.url }}" title="Browse our {{ link.object.title | escape }} collection.">
<img src="{{ link.object.image.src | collection_img_url: 'large' }}" alt="{{ link.object.title | escape }}" data-test="{{ link.object.image.src | collection_img_url: 'large' }}" />
<big>{{ link.title }}</big>
</a>
</div>
{% endfor %}
The strange fact is that it prints two different values for the same object!
<img src="https://cdn.shopify.com/s/files/1/0407/7545/files/trousers-woman_c4633f02-59f7-4a4b-809b-91662635ddc0.jpg?22734" alt="Women's Trousers" data-test="//cdn.shopify.com/s/files/1/0407/7545/collections/DSC_9685_grande_df826b7f-5645-4491-b866-8819c9ad8983_large.jpg?v=1429273629">
The src attribute shows the old image, and the test attribute shows the new one.
Is that because Shopify postprocess the src attributes of the image for caching them into their cdn? How can I fix this?
thanks to #Jason input I found a javascript script that changed the attribute "src" of the image:
$('.collection-woman .webshop .trousers a img').attr('src','https://cdn.shopify.com/s/files/1/0407/7545/collections/DSC_9685_grande_df826b7f-5645-4491-b866-8819c9ad8983_large.jpg?v=1429273629');

Flask: how to link wtforms with jinja2-templates using bootstrap

it seems a stupid question but I do not really get the connection.
I use tghe latest bootstrap version ad have this template:
<div class="form-group"{% if form.name.errors %} error{% endif %}>
<label for="name" class="col-md-2 control-label">Name:</label>
<div class="col-md-10">
<input type="text" class="form-control" id="name" placeholder="Enter your name here">
{% for error in form.name.errors %}
<span class="help-inline">[{{ error }}]</span><br>
{% endfor %}
</div>
</div>
The field is rendered as desired but no matter what I enter there my form will raise the error, that this field is required.
Please let me know what I miss here - how do I link the field in the template to my form properly?
The issue was that the "name" attribute was missing - only having the "id" is insufficient.