0

With a simple model:

class Card(models.Model):
    name = models.CharField(max_length=25)
    rate = models.IntegerField(default=0)
    good = models.BooleanField(default=False)

it is possible to generate a form with:

class CardForm(ModelForm):
    class Meta:
        model = Card
        fields = '__all__'

which then includes all the model fields through a simple template with major part as:

<table>
  {{ form.as_table }}
</table>

But when showing through a ListView subclass as:

class CardShow(ListView):
    model = Card
    template_name = 'myapp/show.html'

it still requires a template where all the model fields are explicitly named as:

<ul>
  {% for card in object_list %}
    <li>{{ card.name }}, {{ card.rate }}, {{ card.good }}</li>
  {% endfor %}
</ul> 

Is there any way to auto-generate HTML code for view (show), just like the form has form.as_table, form.as_p, and form.as_ul?

EquipDev
  • 5,573
  • 10
  • 37
  • 63
  • What do you mean by `auto-generate`? Do you want just iterate through all fields in model or want some one-tag solution to display your fields? – GwynBleidD Sep 29 '15 at 12:00
  • @rami: Yes, I see it is a duplicate; thanks. This also means that there is not easy way for viewing model instance, which is pretty weird when there is a nice way of editing model instance through `ModelForm`. I will mark for close as duplicate. – EquipDev Sep 29 '15 at 12:10
  • @GwynBleidD: Either could do, but I hoped there was some one-tag solution like for `ModelForm`, but probably not since there was no nice solution to the duplicate pointed out by rami. – EquipDev Sep 29 '15 at 12:12
  • There is no simple solution because for displaying forms there are some standard ways, but for displaying lists - almost every view will do it in different way, depending what you're trying to achieve. – GwynBleidD Sep 29 '15 at 12:23
  • @GwynBleidD: OK, thanks for the clarification. I am new to Django, so still trying to learn the concepts and common way of doing different tasks. – EquipDev Sep 29 '15 at 19:39

0 Answers0