Render image in left and right side of the screen alternatively - django-templates

I want to render image in left and right side of the screen alternatively, dynamically in django template. I am not sure how to render it, like if it is 0th for loop counter the image should appear on the left of the screen, for the next iteration the image should render in right and so on.
This is my requirement
This is what I have achieved
Code below
HTML
{% block content %}
<!-- Banner -->
<div class="banner">
<div class="container-fluid banner-content">
<h3>{{page.banner_head}}</h3>
{{page.banner_desc|richtext}}
</div>
</div>
<section class="solution-section">
<div class="container-fluid">
{% for i in page.solutions.all %}
<div class="row pb-5">
<!-- Image -->
<div class="col-md-6">
<div class="solution-image">
<figure class="text-center">
<h6>{{ i.img_text }}</h6>
</figure>
{% image i.sol_img original as img %}
<img src="{{ img.url }}" class="img-fluid solution-index-image" alt="{{ img.alt }}">
</div>
</div>
<!-- Text -->
<div class="col-md-6 solution-desc">
<h5>{{i.sol_head}}</h5>
{{i.sol_desc|richtext}}
learn more
</div>
</div>
{% endfor %}
</div>
</section>
{% endblock %}
models.py
class SolutionPage(Page):
banner_head = models.CharField('Banner Title', blank=True, max_length=255)
banner_desc = RichTextField('Banner Description', blank=True)
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('banner_head'),
FieldPanel('banner_desc'),
], heading='Banner Section'),
InlinePanel('solutions', label='Solution Details'),
]
class Solution(Orderable):
sol_img = models.ForeignKey(
'wagtailimages.Image',
null = True,
blank = True,
on_delete = models.SET_NULL,
related_name = '+',
verbose_name = 'Solution Image',
)
img_text = models.CharField('Image Text', blank=True, max_length=255)
sol_head = models.CharField('Solution Heading', max_length=100, blank=True)
sol_desc = RichTextField('Solution Description', blank=True)
sol_link = models.CharField('Button Link', max_length=255, blank=True)
page = ParentalKey('SolutionPage', related_name='solutions')
panels = [
ImageChooserPanel('sol_img'),
FieldPanel('img_text'),
FieldPanel('sol_head'),
FieldPanel('sol_desc'),
FieldPanel('sol_link')
]

Check odd/even in the template with {% if forloop.counter|divisibleby:"2" %}.

Related

'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 %}

I want to show Filtered products in Vue Js based upon the selection made by user in 3 diffrent parameters from All products

This is step 1 which will appear & user will select one category from this
<div class="w-100 text-center step-div py-5">
<h3>step 1 </h3>
<p class="step-p"> for Men, Women or Unisex</p>
<div class="container">
<div id="categorytype" class="responsive">
<div v-for="(type,typeIndex) in types" class="filter-slider-div col-4 p-5" #click= "addfiltertype(typeIndex,type.typeslug)">
<img :src="type.typeImage" class="filter-img mx-auto">
<p>{{type.typename}}</p>
</div>
</div>
</div>
</div>
Out put will be something like Men,Women,Both,Kids etc & On click It is saving slug of this type. which I'll share below,
<h3>step 2 </h3>
<p class="step-p">Select your case size</p>
<div class="container">
<div id="categorydials" class="responsive">
<div v-for="(dial,dialIndex) in dials" class="filter-slider-div col-4 p-5" #click= "addfilterdial(dialIndex,dial.dialname)">
<img :src="dial.dialimage" class="filter-img mx-auto">
<p>{{dial.dialname}}mm</p>
</div>
</div>
</div>
In step 2 User will select Size it can be anything,
<h3>step 3 </h3>
<p class="step-p">Select your case colour</p>
<div class="container">
<div id="categorycolors" class="responsive">
<div v-for="(color,colorIndex) in colors" class="filter-slider-div p-5" #click= "addfiltercolor(colorIndex,color.colorslug)">
<img :src="color.colorimage" class="filter-img mx-auto">
<p>{{color.colorname}}</p>
</div>
</div>
</div>
In Step 3 User will select Colour
Now After this I am updating & saving the slug of these categoies in my js in method
Here is code how my Categories are coming from & saving the category slug in typeslugFilter
I used alert Js to check if it's working & saving correctly or not & It's Saving the exact slug,
var filters = new Vue ({
el: '#categorytype',
data:{
types: [
{
typeId: 1,
typename: 'Men',
typeImage: 'assets/images/filter1.png',
typeslug: 'men',
},
{
typeId: 2,
typename: 'Women',
typeImage: 'assets/images/filter2.png',
typeslug: 'women',
},
{
typeId: 3,
typename: 'Watches',
typeImage: 'assets/images/filter3.png',
typeslug: 'both',
}
] },
methods: {
addfiltertype (typeIndex,typeslug) {
this.typeslugFilter = typeslug;
// alert(this.typeslugFilter+ '+' + typeslug);
}
}
});
Consider similar code for other two categories with variable dialnameFilter & colorslugFilter,
Now I am trieng to fetch filtered products using below code it's not working for me as of now
I am getting my products from json file & they are working fine I am using typeslug & dialname as category type in single product details & using productslider as el: element .
<div id="productslider" class="container py-5">
<p>{{typeslugFilter}}</p>
<div v-for="(variant,variantIndex) in variants" v-if="typeslugFilter === typeslug && dialnameFilter === dialname">
<div class="watch-slider-div">
<img :src="variant.variantImage" class="w-100 py-3">
</div>
</div>
</div>
Please help me out in this If any tech geek an help will be appreciated

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
})

Phalcon assign view variable inconsistent

Having a problem getting consistent behavior assigning variables to the view. For example:
In controller:
$this->view->media = Media::findFirst(['groupId=0', 'order' => 'RAND()', 'limit' => 1]);
In view:
{% if media is defined %}
<div class="thumbnail">
<img src="{{ static_url('img/media/thumbs/' ~ media.name) }}" class="img-round">
<div class="caption">
<h3>{{ media.title }}</h3>
<p>{{ media.description }}</p>
</div>
</div>
{% endif %}
Which is parsed like:
<?php if (isset($media)) { ?>
<div class="thumbnail">
<img src="<?php echo $this->url->getStatic('img/media/thumbs/' . $this->media->name); ?>" class="img-round">
<div class="caption">
<h3><?php echo $this->media->title; ?></h3>
<p><?php echo $this->media->description; ?></p>
</div>
</div>
<?php } ?>
The problem is that when the parsed version of the template, $media is not accessible via $this so the isset($media) passes, but the references to $this->media returns nothing.
Any way to force $media to be local in scope?
I think I got it.
Misbehaviour description
You have probably declared a media module in your DI(). I was trying quite a lot to reproduce that error, and got it finally when i set a dumb media service among configuration files:
$di->set('media', function() {
return new \stdClass();
});
and than got the same behavior you have - Volt during compilation is not sure, what variable to use and choses $this->media (DI::get('media')) instead of $media or $this->view->media var for obtaining data.
Solution
If you dont want to resign from calling you findFirst result under that variable name, you may bypass that by using View in volt directly:
{% if view.media is defined %}
<div class="thumbnail">
<img src="{{ static_url('img/media/thumbs/' ~ view.media.name) }}" class="img-round">
<div class="caption">
<h3>{{ view.media.title }}</h3>
<p>{{ view.media.description }}</p>
</div>
</div>
{% endif %}
it will generate $this->view->media calls instead of $this->media ones.
+1 on that question.

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

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 =)