Raw query returns 1 row but outputting multiple rows in template

31 views
Skip to first unread message

Thomas Brightwell

unread,
Oct 6, 2016, 7:27:22 AM10/6/16
to Django users
Hi,

I have a raw query returning one row which is then shown 8 times in the template. I have debugged and there is only one line when this is executed in the shell and I do a simple for loop print output. So it seems to be something about the template tags?

The raw query which definitely only returns one row (when executed in shell, mysql direct, debugged in pycharm):

    digital_clients = DigitalClient.objects.raw('''select
        dig.id,
        dig.client,
        dig.digital_campaign_spend,
        dig.job_page_impressions,
        dig.event_page_impressions,
        dig.profile_page_impressions,
        dig.job_page_redirect_impressions,
        dig.event_page_redirect_impressions,
        dig.profile_page_redirect_impressions,
        dig.applications,
        dig.cost_per_application,
        dig.cost_per_hire,
        dig.hires,
        dig.percentage_complete,
        dig.offset_dotmailer_reach,
        dig.offset_dotmailer_clicks,
        sum(cam.client_link_clicks) as 'client_link_clicks',
        sum(cam.reach) as 'reach'
        from dashboard_digitalclient dig
        left join dashboard_dotmailercampaign cam on dig.id = cam.client_id
        group by dig.id
        order by dig.client''')

Then of course in the view I'm passing it into the context for the template:

digital_clients = get_digital_client_summary() 
    context = {
        'digital_clients': digital_clients,
        'last_refreshed': last_refreshed
    }
return render(request, 'dashboard/all_clients_summary.html', context)

Then I'm doing this in the template:

            {% for client in digital_clients %}
            <tr>
                <td><a href="{% url 'single_client_dashboard' client.id %}">{{ client.client }}</a></td>
                <td>{{ client.reach|default_if_none:0|add:client.offset_dotmailer_reach }}</td>
                <td>{{ client.client_link_clicks|default_if_none:0|add:client.offset_dotmailer_clicks }}</td>
                <td>{{ client.job_page_impressions }}</td>
                <td>{{ client.event_page_impressions }}</td>
                <td>{{ client.profile_page_impressions }}</td>
                <td>{{ client.job_page_redirect_impressions }}</td>
                <td>{{ client.event_page_redirect_impressions }}</td>
                <td>{{ client.profile_page_redirect_impressions }}</td>
                <td>£{{ client.digital_campaign_spend }}</td>
                <td>{{ client.percentage_complete }}%</td>
                <td>{{ client.applications }}</td>
                <td>{{ client.hires }}</td>
                <td>£{{ client.cost_per_application }}</td>
                <td>£{{ client.cost_per_hire }}</td>
                <td><a href="{% url 'all_clients_summary' %}?refresh=1&client={{ client.id }}">Client refresh</a></td>
            </tr>
            {% endfor %}

And I get 8 rows in my table?

Help would be much appreciated as I'm super stuck on this one...

All the best,

Tom 



Andromeda Yelton

unread,
Oct 6, 2016, 1:04:39 PM10/6/16
to django...@googlegroups.com
What's in the get_digital_client_summary() function?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dabd210b-06cf-4d28-8e36-a48e8d5cb7c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Andromeda Yelton
Vice President/President-Elect, Library & Information Technology Association: http://www.lita.org
Reply all
Reply to author
Forward
0 new messages