djangodjango-viewsdjango-templatesdjango-tables2django-simple-history

Django - display multiple views in one template


I am currently working on a Django project, in which I want to build a kind of dashboard, which may have actually two bootstrap columns (in the future maybe 3-4). In each column, content from a database is to be displayed. Currently, this means that I want to show two tables. One which shows all entries for a model and a table which shows the last history entries (django-simple-history). The tables are rendered using django-table2.

First of all I tried to solve the problem with the MultiTableMixin. Unfortunately without success. My current idea is to logically split the content into two views and then add a view for each column. I tried to have the views displayed inside iframes. Also, this not working as good as thought.

What are the options for my problem. Or am I thinking too complicated and getting lost?


Solution

  • Your headline is a bit misleading since your issue is actually quiet simple:
    Just make two queries within your view to get two query objects with which you render the columns/tables. So you have one view (function) but the logic is still seperated content wise:

    def get_data_for_columns(request):
        column_all_data = YourModel.objects.all()
        column_latest_data = YourModel.objects.all().order_by('-publish_date')[10:] # 10 latest objects by publish date
    
        context = {
            'all_data': column_all_data
            'latest_data': column_latest_data
        }
        
        return render(request, 'your_template.html', context)
    

    template:

    # example loop data and render table
    
       <table>
         <tr>
           {% for item in all_data %}
    
           <td>{{ item.value }}</td>
    
           {% endfor %}
         </tr>
       </table>
    
       <table>
         <tr>
           {% for item in latest_data %}
    
           <td>{{ item.value }}</td>
    
           {% endfor %}
         </tr>
       </table>