2

I'm displaying data on a webpage, and I would like to migrate this code to use the table I created in tables.py. I can't figure out how to do it without breaking the filter.

views.py

def PlatListView(request):
    
    queryset = Plat.objects.all().values('id', 'description','status', 'phase__number','phase','schedule_found').annotate(lot_count=Sum('phase__lot_count')).order_by('description')

    f = PlatFilter(request.GET, queryset=queryset)
   
    return render(request, 'blog/filtertable2.html', {'filter': f})

filters.py

class PlatFilter(django_filters.FilterSet):
    community = ModelChoiceFilter(queryset=Community.objects.all())

tables.py

import django_tables2 as tables

class PlatTable(tables.Table):  

    id = tables.Column()
    description = tables.Column()
    status = tables.Column()
    phase__number = tables.Column()
    lot_count = tables.Column()
    schedule_found = tables.Column()
    
    class Meta:
        ordering = 'description'
        #model = Plat

filtertable2.html

{% extends "blog/base.html" %}
{% block content %}

{% load bootstrap4 %}

<form method="get">
        {{ filter.form.as_p }}
        <input type="submit" />
    </form>

 <table>
  <tr>
    <th>Description</th>
    <th>Status</th>
  </tr>
 {% for obj in filter.qs %}
  <tr>
    
        <td> {{ obj.description }}</td>
        <td>{{ obj.status }}</td>
   
  </tr>
   {% endfor %}
</table>
   
{% endblock content %}
2
  • How do you render the template? Can you provide the relevant parts of the template? Commented Dec 23, 2020 at 17:35
  • @WillemVanOnsem added the html Commented Dec 23, 2020 at 17:42

2 Answers 2

4

In your view you construct the table with the data from the filter:

def PlatListView(request):
    queryset = Plat.objects.annotate(
        lot_count=Sum('phase__lot_count')
    ).order_by('description')

    f = PlatFilter(request.GET, queryset=queryset)
    table = PlatTable(data=f.qs)
    return render(request, 'blog/filtertable2.html', {'filter': f, 'table': table})

You can then render the table with:

{% extends "blog/base.html" %}
{% block content %}

{% load bootstrap4 %}
{% load render_table from django_tables2 %}

<form method="get">
    {{ filter.form.as_p }}
    <input type="submit" />
</form>

{% render_table table %}
   
{% endblock content %}
Sign up to request clarification or add additional context in comments.

3 Comments

got it, thanks. And how do I Include the filter in the template?
@MattWilson: the same way as before. Forgot about that. Updated the answer.
@MattWilson: also made a typo in the view, filter of course still refers to f, not f.qs.
1

I do not see any particular reason for using Values QuerySet, ie values(). You can simply annotate the value with queryset:

def plat_list_view(request):  # using snake_case when defining a method name.
    queryset = Plat.objects.annotate(lot_count=Sum('phase__lot_count')).order_by('description')
    f = PlatFilter(request.GET, queryset=queryset)
    return render(request, 'blog/filtertable2.html', {'filter': f})

Comments

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.