[Fixed]-Django 1.8 calls to database in html code

1👍

Overall, it is a not such a good idea. You are sending all data to the template engine and doing the filtering there?

Why not filter it in the view function / view class and then return that data inside a template variable and then render in the front end?

def detail(request, poll_id):       
    filtered_data = .......objects.get(name='look')
    return render(request, 'polls/detail.html', {'look_data': filtered_data})

{% for article, section in look_data %}
<div class="media panel panel-default">
    .... blah blah blah
</div>
{% endfor %}

0👍

As I understand, you just need to add if statement:

{% for article, section in featured_articles %}
{% if section.name == 'look' %}
<div class="media panel panel-default">
    <div class="panel-body">
        <div class="media-left">
            <a href="articles/{{ article.url }}">
                <img class="media-object thumbnail-featured"
                     src="{{ article.image }}">
             </a>
        </div>
        <div class="media-body">
            <a href="articles/{{ article.url }}">
                <h3 class="media-heading">{{ article.title }}</h3>
            </a>


                <!-- TODO figure out how to iterate through the authors field, manytomany -->
                {% for contributor in article.authors.all %}
                <p><a href="/{{ section.url }}">{{ section.name }}</a> |
                    <a href="/contributors/{{ contributor.twitter }}">{{           contributor }}</a> </p>
                    {% endfor %}

                          <p>{{article.preview}}</p>
        </div>
    </div>
</div>
{% endif %}
{% endfor %}

Leave a comment