0

I want to show a table containing a set of model objects.

My model class:

from django.dbfrom django.db import models
from django.utils.translation import ugettext_lazy as _

class DamageKind(models.Model):

    name = models.CharField(_('damage kind'), max_length=64)
    regions = models.ManyToManyField(Region)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('damage kind')
        verbose_name_plural = _('damage kinds')

my form class:

from django import forms

from crispy_forms.helper import FormHelper

from .models import DamageKind


class DamageKindList(forms.Form):

    def __init__(self, *args, **kwargs):
        self.damagekinds = kwargs.pop('damagekinds', [])
        self.helper = FormHelper()
        self.helper.form_method = 'post'
        super().__init__(*args, **kwargs)

My base template base.html:

<!DOCTYPE html>
<html>
  <body>
    {% block list %}

    {% endblock %}
  </body>
</html>

my list_damagekinds.html:

{% extends "./base.html" %}                                                                   
{% load crispy_forms_tags %}                                                                  

{% block list %}                                                                              
     <form action="" method="post">                                                           
       {% csrf_token %}                                                                       
       {{ damagekind_form }}                                                                  
     </form>                                                                                  
{% endblock %}

and my views.py:

def list_damagekinds(request):
    damagekinds = DamageKind.objects.all()
    return render(
        request,
        'damage/list_damagekinds.html',
        {'damagekind_form': DamageKindList(damagekinds=damagekinds), }
    )

so my question is how I can make a table containing all the names of the damagekinds by not beeing editable, so just showing these. And with using the crispy_forms FormHelper and not code it into the template.

Thanks in advance.

1 Answer 1

1

my list_damagekinds.html

{% extends "./base.html" %}                                                                   
{% load crispy_forms_tags %}                                                                  

{% block list %}                                                                              
     <form action="" method="post">                                                           
       {% csrf_token %}               

       <table>                                                 
          {% for obj in damagekind_form %}        
             <tr> {{ obj.name }} </tr>
          {% endfor %}
       </table>      

     </form>                                                                                  
{% endblock %}
Sign up to request clarification or add additional context in comments.

2 Comments

but that looks not like the nicest way, shouldn't that be made by the FormHelper/DamageKindList-form?
As this values are not meant to be part of any http command (GET, POST, PUT, DELETE) you may want to perform, where is the point in rendering it as a form? This is just a simple example - you can redesign it and add bootrap-classes or other css to it for a nicer look if it is that what concerns you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.