简体   繁体   中英

Model to view as dictionary then render it as a html table

Im learning Django and get stuck with this problem. I need to list "users" with their respective data to an html list. I tried but there is no list generated. Already have my db populated with 100 lines.

This is my models.py

from django.db import models
# Create your models here.


class General(models.Model):
    id = models.CharField(max_length=128, primary_key=True)
    name= models.CharField(max_length=128)
    lastname= models.CharField(max_length=128)
    state= models.CharField(max_length=128)
    asd= models.CharField(max_length=128)
    spc= models.CharField(max_length=128)
    dot = models.CharField(max_length=128, blank=True)

    def __str__(self):
        return self.name

This is my views.py, already tried return render_to_response , and tried to use General.objects.all()/General.objects.get() but it says

General has no objects

so i cant make the dictionary manually

from django.shortcuts import render
from Myapp.models import General
from django.forms.models import model_to_dict
# Create your views here.


def VisualizadorGeneral(request):
    listaGeneralDic = model_to_dict(General)
    return render(request, "VisualizadorGeneral.html", context=listaGeneralDic)

And this is my HTML:

<div class="container">
            {% if listaGeneralDic %}
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>id</th>
                        <th>name</th>
                        <th>lastname</th>
                        <th>state</th>
                        <th>asd</th>
                        <th>spc</th>
                        <th>dot</th>
                    </tr>
                </thead>
                <tbody>
                    {% for dato in listaGeneralDic %}
                    <tr>                        
                        <td scope="row">{{ dato.id }}</td>
                        <td>{{ dato.name }}</td>
                        <td>{{ dato.lastname }}</td>
                        <td>{{ dato.state }}</td>
                        <td>{{ dato.asd }}</td>
                        <td>{{ dato.spc }}</td>
                        <td>{{ dato.dot }}</td>                        
                    </tr>
                    {% endfor %}
                 </tbody>
                 {% else %}
                 <p>Nothing to see</p>    
                 {% endif %}
            </table>
        </div>

Someone can spot what im doing wrong?I was been 3 days lurking the whole internet to find answers.

A class is a "blueprint" that can be used to "instantiate" different "objects". It only specifies the different properties and methods. But in template we need to show a list of real objects.

def VisualizadorGeneral(request):
    objects_list = General.objects.all() # here we are retrieving real instances
    list_of_object_dicts = [model_to_dict(obj) for obj in objects_list] # and in this row we are applying function model_to_dict for each object
    context_data = {"listaGeneralDic: list_of_object_dicts} # context should be a dict with keys and values, after announcing "listaGeneralDic" in context we will be able to call it in template
    return render(request, "VisualizadorGeneral.html", context=context_data)

You can also use Django's class-based List View

to have all the General data objects shoved to the template as a context object for you. It will also take care of rendering and get/post for you

from django.views.generic import ListView

class VisualizadorGeneral(ListView)
   template_name = '<your_template_name>'
   model = General
   context_object_name = 'dto' 
   # this is the name you will refer to the object in the template

you can refer to in your template like this

 {% for item in dto %}
    <td>{{ item.<whatever> }}</td>
 {% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM