set a variable in django template file, and update it after iteration - django

Trying to set variable in django template, and make a simple rule to update it after iteration. Here is my template:
{% for adv in advs %}
<div class="media-item big" style="top: 18%;left:{% cycle '304' '1078' %}px;">
<div class="media-item__tags">
{{ adv.year }}
{{ adv.payer}}
</div>
<div class="media-item__content">
<div class="media-item__background">
<div class="media-item__canvas">
<div class="media-item__canvas-background" style="background-image: url({{adv.image.url}})"></div>
</div>
<h2 class="topic white upcase fixed-size">{{ adv.name }}</h2>
Смотреть проект
</div>
</div>
</div>
In first div i need to make different 'left:' value. I want to make rule: after every iteration, value changes from base=304 to base+774 px. I tryed to do it somehow with {% cycle %} but it doesnt work for me, also tryed to set variables with {% with %} tag, but didnt find any information about how to update them.

You can set the style by multiplying the current counter from 0...n with 774 and add base value 304. For this, you'll need a custom template tag.
Create a templatetags directory in your app. Add an empty __init__.py and multiply_add.py.
multiply_add.py
from django import template
register = template.Library()
#register.simple_tag
def mul_add(a, b, base_value):
return (a * b) + base_value
template.html
{% load multiply_add %}
{% for adv in advs %}
<div class="media-item big" style="top: 18%;left:{% multiply_add forloop.counter0 774 304 %}px;">
<div class="media-item__tags">
{{ adv.year }}
{{ adv.payer}}
</div>
<div class="media-item__content">
<div class="media-item__background">
<div class="media-item__canvas">
<div class="media-item__canvas-background" style="background-image: url({{adv.image.url}})"></div>
</div>
<h2 class="topic white upcase fixed-size">{{ adv.name }}</h2>
Смотреть проект
</div>
</div>
</div>

Related

How to loop over Chart.js with Django list view

I am using a Django List View, and I am trying to iterate through multiple objects and display percentage values in a Chart.JS gauge.
However, although I am iterating the names of the gauge id's by using a for loop counter, I am only ever getting the first iteration of the chart.js object rendering on my screen. My initial thoughts are that similar to how I am dynamically creating new canvas ids for the chart.js objects, I should be doing a similar thing for the variable I am trying to pass into the chart object e.g. reduction overall, but I am not having any luck. Your feedback is welcome.
Views.py
class PerformanceDetailView(ListView):
template_name = 'performance_detail.html'
model = Performance
context_object_name = 'performance'
def get_queryset(self, **kwargs):
code = self.kwargs.get('code')
return Performance.objects.filter(code=code)
Performance_detail.html
<section class="projects pt-4 col-lg-12">
{% for item in performance %}
<div class="container-fluid pt-2 col-lg-12">
<!-- Project--><div class="project" >
<div class="row bg-white has-shadow" style="height: 14rem">
<div class="left-col col-lg-2 d-flex align-items-center justify-content-between">
<div class="project-title d-flex align-items-center">
<div class="has-shadow"><img src="{% static 'img/avatar-2.jpg' %}" alt="..." class="img-fluid"></div>
</div>
</div>
<div class="right-col col-lg-2 align-items-center vertical-center">
<div class="text">
<h3 class="h2 pt-2">{{item.brand}} {{item.style}}</h3>
<p class="text-muted">{{item.package_type| capfirst}} package, round {{item.testing_round}}</p>
<p class="text-muted">Item size: {{item.size}}</p>
</div>
</div>
<div class="right-col col-lg-8 align-items-center vertical-center">
<div class="row mt-5">
<div class="col-md-3">
<div class="gauge-container">
<canvas id="gauge1-{{ forloop.counter }}" class="gauge-canvas"></canvas><span id="gauge1Value-{{ forloop.counter }}" class="gauge-label"></span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{{ item.reduction_overall|json_script:"reduction_overall" }}
{{ for_loop_counter|json_script:"for_loop_counter" }}
{% endfor %}
</section>
{% endblock content %}
Block JS within package_detail.html
<!-- Gauge.js-->
<script src="{% static 'vendor/gaugeJS/gauge.min.js' %}"></script>
<script>
$(function () {
var reduction_overall = JSON.parse(document.getElementById('reduction_overall').textContent);
var for_loop_counter = JSON.parse(document.getElementById('for_loop_counter').textContent);
// Gauges
var gauge1 = document.getElementById('gauge1-'+for_loop_counter);
opts.colorStop = "#864DD9";
var gaugeObject1 = new Donut(gauge1).setOptions(opts);
gaugeObject1.maxValue = 100; // set max gauge value
gaugeObject1.setMinValue(0); // set min value
gaugeObject1.set(reduction_overall); // set actual value
gaugeObject1.setTextField(document.getElementById("gauge1Value-" + for_loop_counter));
});
</script>
{% endblock js %}
This was solved by including the JavaScript in the main block content and dynamically naming the gauge elements themselves.
var func_name = "container-{{forloop.counter}}";
func_name = new Donut(gauge1).setOptions(opts);
I posted a full example to a similar problem here Chart JS and Django loop

Showing list of items including item details on same page in Django

In a Django v3.x app I would like to display a list of uploaded file names (e.g. images) in the left hand side of the screen. When a user clicks on one of those, I'd like to display the actual file/image on the right hand side of the screen. I am still new to Django and have used both ListView and DetailView separately, but not in such a combination. I'm not sure how this can be achieved.
Using a little Bootstrap magic, I can create a split screen easily. Hence, my template would look somehow like this:
<div class="row">
<div class="col-md-5 left">
{% for image in images %}
<div class="card">
<h4>{{ image.url }}</h4>
View
</div>
{% endfor %}
</div>
<div class="col-md-5 right">
{# TODO: When the user clicks on the View url above, then I'd
like to load the actual image here on the right hand side of the
screen inside this div-tag. #}
</div>
</div>
Question 1: How can I achieve loading a selected image from a list? Can I still use ListView and DetailView, or do I need to write my own View logic?
Question 2: Ideally, I'd like to NOT re-send the whole page from the server to the client, because the list of images in the lefthand-side could potentially be long and require pagination. So, when the user clicks View, then, ideally, I'd like to load only the document from the server. Is this somehow feasible?
Well I have made a sample code and you can refer to it and get some idea.
<!--Carousel Wrapper-->
<div id="carousel-thumb" class="carousel slide carousel-fade carousel-thumbnails"
data-ride="carousel">
<!--Slides-->
<div class="row">
<div class="col-lg-8">
<div class="carousel-inner" role="listbox">
{% for latest in latest_course %}
<div class="carousel-item {% if forloop.counter0 == 0 %}active{% endif %}">
<img class="d-block w-100" src="{{latest.poster.url}}" alt="First slide">
</div>
{% endfor %}
</div>
</div>
<!--/.Slides-->
<div class="col-lg-4">
<ol class="slider_list">
{% for latest in latest_course %}
<li data-target="#carousel-thumb" data-slide-to="{{forloop.counter0}}"
class="active"> <img class="img-thumbnail" width="100px" height=100px src="
{{latest.poster.url}}"
class="img-fluid"></li>
{% endfor %}
</ol>
</div>
and the output looks like this
Here, you can see the list on the right side and when you select an item the selected item loads in the left side. This way you can style your template the way you want.
basically you want to show the selected item from the list on the other side like a slideshow

Django : Factorize HTML code

I am building a website about movies, actors etc... And I need your advices in order to factorize my templates.
Currently I have 5 templates :
base.html : containing header menu, footer [located in the master templates folder]
actors : display all the actors (extends base.html) [located in the templates folder from the application 'movie-library']
actor : display one actor and all tbe movies he played in (extends base.html) [located in the templates folder from the application 'movie-library']
movies : display all the movies (extends base.html) [located in the templates folder from the application 'movie-library']
movie : display one movie (extends base.html) [located in the templates folder from the application 'movie-library']
So, in actor.html and movies.html it's the same HTML code to display one movie.
And in actors.html and movies.html I have the same HTML to display a pagination bar.
For example here is the code used to display a movie :
<div class="col s3 module">
<div class="card movie-card">
<div class="card-image">
<img src="{{ MEDIA_URL }}{{ movie.photoshoot.folder }}/1.jpg" alt="{{ movie.title }} thumbnail">
</div>
<div class="card-content">
<span class="card-title grey-text text-darken-4">
{{ movie.title }}
</span>
<p>
<i class="material-icons blue-text accent-2-text">hd</i>
{% if movie.movie is not None %}
<i class="material-icons blue-text accent-2-text">local_movies</i>
{% endif %}
{% if movie.photos is not None %}
<i class="material-icons blue-text accent-2-text">image</i>
{% endif %}
<span class="grey-text darken-1-text right">{{ movie.movie.duration }}</span>
</p>
</div>
</div>
</div>
How could I factorize those to piece of code ?
Thank you.
You can use include tag for movies and custom tag for pagination

get_profile alternative in for loop django

I'm trying to get information of a users avatar based in an extended profile model. I usually call the information via get_profile(). However on this occasion the call is within a for loop in the template and I get errors if one of the users are the same.
How would I go about avoiding this error?
{% for fevent in fevents %}
<!-- EVENT ><! -->
<div class="event">
<div class="pic">
{{ fevent.getPublishedPredictionsCount }}
<img src="{% thumbnail fevent.artwork.get_absolute_url 100x98 crop,upscale %}" alt="" width="100" height="98" />
<div class="overlay">
</div>
</div>
<h1>{{ fevent.title|trunchar:30 }}</h1>
{% autoescape off %}
{{ fevent.getEventPredictionScore|makestars }}
{% endautoescape %}
<ul class="details">
<li class="cat">
Category: {{ fevent.catagory }}
</li>
<li class="location">
{{ fevent.location }}
</li>
<li class="date">
{{ fevent.date_and_time }}
</li>
<li class="time">
7:00pm - 8:00pm
</li>
</ul>
<!-- CLEAR ><! --><div class="clear"></div>
<div class="hype">
<div class="avatar">
<img src="{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}" alt="" width="120" height="120" />
</div>
<p>{{ fevent.description|trunchar:200 }}… Read More</p>
</div>
<!-- CLEAR ><! --><div class="clear"></div>
</div>
<!-- END OF EVENT ><! -->
{% endfor %}
The problem is here:
{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}
Error Message returned:
Caught MultipleObjectsReturned while rendering: get() returned more than one UserProfile -- it returned 2! Lookup parameters were {'user__id__exact': 4L}
To expand on what Matt said, while using get_or_create is a good idea, you should definitely define your profile model's User link with a OneToOneField, instead of a ForeignKey.
user = models.OneToOneField(User, verbose_name=_(u'user'))
Now, if you forget to use get_or_create(), or somehow accidentally try to create a duplicate profile for the same user, the database will raise an IntegrityError.
That error means there are two UserProfile objects in the database matching the query used by get_profile, not that get_profile was called twice. You need to remove one of those profile objects from the database and make sure there aren't multiples created again. You should be able to use the get_profile method multiple times without issue. Maybe you have a get_or_create call in that function without checking the proper values.

UnicodeDecodeError in template

I get the following error code when trying to load the template.
'utf8' codec can't decode byte 0x94 in position 720: invalid start byte
Here is the template:
{% extends "base.html" %}
{% block site_wrapper %}
<div id="main">
Skip to main content
<div id="banner">
<div class="bannerIEPadder">
<div class="cart_box">
[link to cart here]
</div>
Modern Musician
</div>
</div>
<div id="navigation">
<div class="navIEPadder">
[navigation here]
</div>
</div>
<div id="middle">
<div id="sidebar">
<div class="sidebarIEPadder">
[search box here]
<br/>
[category listing here]
</div>
</div>
<div id="content">
<a name=”content”></a>
<div class="contentIEPadder">
{% block content %}{% endblock %}
</div>
</div>
</div>
<div id="footer">
<div class="footerIEPadder">
[footer here]
</div>
</div>
</div>
{% endblock %}
In UTF-8 0x94 is nothing, however in ISO1252 it's a right quote (”). Generally speaking the plain quote (") is much safer.
Make sure you're not copying and pasting this out of some blog that has weird accented quotes or something like that.
If you're using a text editor save it as ascii and see what crops up missing.
You have weird double quotes around div#content, try replacing them with ASCII quotes.
Maybe your template is encoded with something other than utf-8? It depends on your terminal/editor or maybe OS settings.
I had some strange characters in my code because i copied out of a pdf-file.
I had this same error . . . and it turned out that the problem was I included a "©" in my source copied as a part of a template.
Got to check that code for strange characters.........