开发者

Displaying a Table in Django from Database

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 approach. More specifically, how do you pretty 开发者_如何学编程much port over the columns and rows in a database table to a visual table that can be seen from a url?


The easiest way is to use a for loop template tag.

Given the view:

def MyView(request):
    ...
    query_results = YourModel.objects.all()
    ...
    #return a response to your template and add query_results to the context

You can add a snippet like this your template...

<table>
    <tr>
        <th>Field 1</th>
        ...
        <th>Field N</th>
    </tr>
    {% for item in query_results %}
    <tr> 
        <td>{{ item.field1 }}</td>
        ...
        <td>{{ item.fieldN }}</td>
    </tr>
    {% endfor %}
</table>

This is all covered in Part 3 of the Django tutorial. And here's Part 1 if you need to start there.


$ 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 %}
<!doctype html>
<html>
    <head>
        <link rel="stylesheet" href="{% static 
'ticket/static/css/screen.css' %}" />
    </head>
    <body>
        {% render_table istekler %}
    </body>
</html>


If you want to table do following steps:-

views.py:

def view_info(request):
    objs=Model_name.objects.all()
    ............
    return render(request,'template_name',{'objs':obj})

.html page

 {% for item in objs %}
    <tr> 
         <td>{{ item.field1 }}</td>
         <td>{{ item.field2 }}</td>
         <td>{{ item.field3 }}</td>
         <td>{{ item.field4 }}</td>
    </tr>
       {% endfor %}


The answers in this thread rely on manually feeding column names, and I prefer to have some way of viewing a Django model completely by default. I have cooked up the solution below:

views.py

from django.shortcuts import render
from .models import TABLEOFINTEREST

def TABLEOFINTEREST(request):
    MODEL_HEADERS=[f.name for f in TABLEOFINTEREST._meta.get_fields()]
    query_results = [list(i.values()) for i in list(TABLEOFINTEREST.objects.all().values())]
    #return a response to your template and add query_results to the context
    return render(request, "/TABLEOFINTEREST.html", {
            "query_results" : query_results,
            "model_headers" : MODEL_HEADERS
        }) 

TABLEOFINTEREST.html:

<table>
    <tr>
    {% for item in model_headers %}
    <th>{{ item }}</th>
    {% endfor %}
    </tr>
    {% for all_rows in query_results %}
    <tr> 
    {% for every_column in all_rows %}
    <td>{{ every_column }}</td>
    {% endfor %}
    </tr>
    {% endfor %}
</table>

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("TABLEOFINTEREST", views.TABLEOFINTEREST, name="TABLEOFINTEREST")
]

Tested and validated on my machine with multiple tables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜