K
K
Konstantin Gorodetsky2016-11-10 15:00:11
Django
Konstantin Gorodetsky, 2016-11-10 15:00:11

How to create a table from a DetailView using django_tables2?

There are the following models:

class Group(models.Model):
    group_name      = models.CharField(max_length=250, verbose_name='Название объекта', help_text='Название объекта')
    date_create     = models.DateTimeField(verbose_name='Дата создания', auto_now_add=True)
    date_updated    = models.DateTimeField(verbose_name='Дата редактирования', auto_now=True)

class Message(models.Model):
    group           = models.ForeignKey(Group, verbose_name='Объект', null=True, blank=True, related_name='messages')
    message_text    = models.TextField(verbose_name='Замечание', help_text='Замечание')

So far I'm displaying the information like this:
views.py

class DetailView(generic.DetailView):
    model = Group

    def get_object(self):
        object = super(DetailView, self).get_object()
        return object

    def get_context_data(self, **kwargs):
        context = super(DetailView, self).get_context_data(**kwargs)
        context['message_list'] = Message.objects.filter(group=self.get_object())
        return context

urls.py

url(r'^grp/(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

group_detail.html

{% extends "ruik/base.html" %}
{% block head_title %} Детали | {{ block.super }} {% endblock %}
{% block content %}

<h1>{{ group.group_name }}</h1>
<p>
  <li>Время создания: {{ group.date_create }}</li>
  <li>Время изменения: {{ group.date_updated }}</li>
</p>

<p>
{% for message in message_list %}
  <li><a href="{% url 'mssgs:detail' message.id %}">{{ message.message_text }}</a></li>
{% endfor %}
</p>

{% endblock %}

I need instead
{% for message in message_list %}
  <li><a href="{% url 'mssgs:detail' message.id %}">{{ message.message_text }}</a></li>
{% endfor %}

display the table using django_tables2.
Separately, with the Message model through the ListView, everything worked out. Now I'm thinking how to insert the same table here ...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question