1

I am a beginner to python and django. Here I am trying to build a website.

I have created a class named cluster which actually mean a town or city. As a subclass I have created schools for cluster. Schools have a field school_strength to get strength for each school. Now I have to display the total strength of all the schools that belong to each cluster.

This is how I am trying to do that in html template for cluster details by creating a local variable strength to calculate sum from all schools.

<div class="col-sm-4 col-md-3">
        <div class="panel panel-default">
            <div class="panel-body">
                <a href="{% url 'music:cluster_detail' state.id region.id cluster.id %}">
                    {% if cluster.cluster_logo %}
                        <img src="{{ cluster.cluster_logo.url }}" class="img-responsive">
                    {% else %}
                        <h3>No image to display</h3>
                    {% endif %}
                </a>
                <h2>{{ cluster.cluster_name }}</h2>
                <h4>{{ cluster.cluster_coordinator }}</h4>
                <h4>{{ cluster.cco_number }}</h4>
                <h4>{{ cluster.cco_email }}</h4>
                {% for school in cluster.school_set.all %}
                    {% strength = strength + school.school_strength %}
                {% endfor %}
                <h4>{{ strength }}</h4>
            </div>
        </div>
    </div>
3
  • 1
    any particular reason to calculate this in the template, and not python code? Commented Sep 13, 2017 at 18:17
  • 1
    You can't use a variable in the template. You should make template tag that will make this logic for you. But, a better way is to make it use a code in view.py or use ModelManager Commented Sep 13, 2017 at 18:24
  • No particular reason. Can you explain me how can I declare a variable and use it in my view.py. I really appreciate the help. Commented Sep 13, 2017 at 18:45

1 Answer 1

3

I think you can declare the variable with with tag. But that will not work the the way you want it to work. Below is an example to it.

{% with name="World" greeting="Hello" %}     
    <h1>{{ greeting }} {{name}}!</h1>
{% endwith %}

So, the better way is you can store it in a variable in views.py and pass it to html template.

views.py

strength=0
for school in cluster.school_set.all():
     strength = strength + school.school_strength

context['strength']=strength

return render(request, 'template/html`, context)

html

<h3>Strength Of Cluster: {{strength}}</h3>
Sign up to request clarification or add additional context in comments.

4 Comments

That really helped. Thank you.
cluster.school_set.all():
Thats really nice of you.Feel privileged to help you out. If you think this solution helped you , you can select this answer. Thanks again.
Yeah, I forgot that it's not the template. I updated the answer. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.