如何在网页上以表格格式显示数据库表中的信息?有没有一种简单的方法在django中执行此操作,或者它需要更复杂的方法.更具体地说,您如何将数据库表中的列和行几乎移植到可以从URL中看到的可视表?
j_s*_*syk 64
最简单的方法是使用for循环模板标记.
鉴于以下观点:
def MyView(request):
    ...
    query_results = YourModel.objects.all()
    ...
    #return a response to your template and add query_results to the context
你可以添加这样的代码片段你的模板......
<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>
这些都在Django教程的第3部分中介绍.如果你需要从那里开始,这里是第1部分.
Ana*_*Ali 12
$ 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>
| 归档时间: | 
 | 
| 查看次数: | 76944 次 | 
| 最近记录: |