-1

I'd like to create HTML template likes the followings.

・models.py

class Client(Model):
name = CharField(max_length=50)
email = EmailField(max_length=100, verbose_name="E-mail")
title = CharField(max_length=50)
department = CharField(max_length=50)

・Data

| Name | Mail | Title | Department |
| John | john@mailaddress.com | engineer | development |
| Bob | bob@mailaddress.com | engineer | development |
| Sam | sam@mailaddress.com | engineer | development |

・views.py

class myListView(, ListView):
model = Client
template_name = "template.html"

・template.html

       <table>
          <tbody>
          {% for item in object_list %}
            <tr>
              <td>{{ item }}</td>
            </tr>
          {% endfor %}
          <!--- table items end -->
          </tbody>
        </table>

・Expectation(table)

| John | john@mailaddress.com | engineer | development |
| Bob | bob@mailaddress.com | engineer | development |
| Sam | sam@mailaddress.com | engineer | development |

I don't want to write all keys in template file, because my product data has too many columns.

Could you tell me how to make a template please ?

Akira Inamori
  • 109
  • 2
  • 5

1 Answers1

4

Basicly, to render your model in template is like this;

{% for item in object_list %}
  <tr>
    <td>{{ item.name }}</td>
    <td>{{ item.email }}</td>
    <td>{{ item.title }}</td>
    <td>{{ item.department }}</td>
  </tr>
{% endfor %}

But, if you don't want to write all keys in template file. you can alson handle it inside your model. an example:

class Client(Model):
    name = CharField(max_length=50)
    email = EmailField(max_length=100, verbose_name="E-mail")
    title = CharField(max_length=50)
    department = CharField(max_length=50)

    def get_string_fields(self):
        return '%(name)s - %(email)s - %(title)s - %(department)s' % {
            'name': self.name, 'email': self.email, 
            'title': self.title, 'department': self.department}

and then, in your template should be..

{% for item in object_list %}
  <tr>
    <td>{{ item.get_string_fields }}</td>
  </tr>
{% endfor %}

Or, you can using your __str__ (Python3), and __unicode__ (Python2) that returns same like function of get_string_fields(self) above. then, if you render in the template which following {{ item }}, should return what you got from __str__ or __unicode__ function.


Other optional

Use _meta like this answer. model._meta.get_all_field_names() will give you all the model's field names, then you can use model._meta.get_field() to work your way to the verbose name, and getattr(model_instance, 'field_name') to get the value from the model.

NOTE: model._meta.get_all_field_names() is deprecated in django 1.9. Instead use model._meta.get_fields() to get the model's fields and field.name to get each field name. an example:

class Client(Model):
    name = CharField(max_length=50)
    email = EmailField(max_length=100, verbose_name="E-mail")
    title = CharField(max_length=50)
    department = CharField(max_length=50)

    def get_string_fields(self):
        # list of some excluded fields
        excluded_fields = ['id', 'pk']

        # getting all fields that available in `Client` model,
        # but not in `excluded_fields`
        field_names = [field.name for field in Client._meta.get_fields() 
                       if field.name not in excluded_fields]
        values = []
        for field_name in field_names:
            # get specific value from instanced object.
            # and outputing as `string` value.
            values.append('%s' % getattr(self, field_name))

        # joining all string values.
        return ' | '.join(values)
Community
  • 1
  • 1
binpy
  • 3,994
  • 3
  • 17
  • 54