Displaying a Table in Django from Database

前端 未结 3 1361
无人及你
无人及你 2020-12-22 19:26

How do you display the information from a database table in a table format on a webpage? Is there a simple way to do this in django or does it require a more complicated app

3条回答
  •  粉色の甜心
    2020-12-22 20:04

    $ pip install django-tables2
    

    settings.py

    INSTALLED_APPS , 'django_tables2'
    TEMPLATES.OPTIONS.context-processors , 'django.template.context_processors.request'
    

    models.py

    class hotel(models.Model):
         name = models.CharField(max_length=20)
    

    views.py

    from django.shortcuts import render
    
    def people(request):
        istekler = hotel.objects.all()
        return render(request, 'list.html', locals())
    

    list.html

    {# yonetim/templates/list.html #}
    {% load render_table from django_tables2 %}
    {% load static %}
    
    
        
            
        
        
            {% render_table istekler %}
        
    
    

提交回复
热议问题