Comment afficher les informations d'une table de base de données sous forme de tableau sur une page web ? Y a-t-il un moyen simple de faire cela en django ou cela nécessite-t-il une approche plus compliquée. Plus précisément, comment porter les colonnes et les lignes d'un tableau de base de données dans un tableau visuel qui peut être vu à partir d'une url ?
Réponses
Trop de publicités?La méthode la plus simple consiste à utiliser un pour la boucle du modèle de balise .
Compte tenu de la vue :
def MyView(request):
...
query_results = YourModel.objects.all()
...
#return a response to your template and add query_results to the context
Vous pouvez ajouter un extrait comme celui-ci à votre modèle...
<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>
Tout cela est couvert par Troisième partie du tutoriel Django. Et voici Première partie si vous devez commencer par là.
$ 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())
liste.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>
Si vous souhaitez obtenir un tableau, procédez comme suit
views.py :
def view_info(request):
objs=Model_name.objects.all()
............
return render(request,'template_name',{'objs':obj})
Page .html
{% for item in objs %}
<tr>
<td>{{ item.field1 }}</td>
<td>{{ item.field2 }}</td>
<td>{{ item.field3 }}</td>
<td>{{ item.field4 }}</td>
</tr>
{% endfor %}
Les réponses dans ce fil de discussion reposent sur l'alimentation manuelle des noms de colonnes, et je préfère avoir un moyen d'afficher un modèle Django complètement par défaut. J'ai préparé la solution ci-dessous :
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")
]
Testé et validé sur ma machine avec plusieurs tables.