I am trying to render data from 3 models in single html table. I have tried an error few times but failed. Below is my code.
Models:
class Site(models.Model):
site_name = models.CharField(max_length=30, blank=True, null=True, verbose_name="Site Name")
site_address = models.CharField(max_length=30, blank=True, null=True, verbose_name="Site Address")
def __str__(self):
return self.site_name
class Workstation(models.Model):
name = models.CharField(max_length=30, blank=True, null=True, verbose_name="Workstation Name")
serial = models.CharField(max_length=30, blank=True, null=True, verbose_name="Serial")
workstation_model = models.CharField(max_length=30, blank=True, null=True, verbose_name="Workstation Model")
sitename = models.ForeignKey(Site, on_delete=models.SET_NULL, blank=True, null=True, verbose_name="Site")
def __str__(self):
return self.name
class Laptop(models.Model):
name = models.CharField(max_length=30, blank=True, null=True, verbose_name="Laptop Name")
serial = models.CharField(max_length=30, blank=True, null=True, verbose_name="Serial")
laptop_model = models.CharField(max_length=30, blank=True, null=True, verbose_name="Laptop Model")
sitename = models.ForeignKey(Site, on_delete=models.SET_NULL, blank=True, null=True, verbose_name="Site")
def __str__(self):
return self.name
class Printer(models.Model):
name = models.CharField(max_length=30, blank=True, null=True, verbose_name="Printer Name")
serial = models.CharField(max_length=30, blank=True, null=True, verbose_name="Serial")
printer_model = models.CharField(max_length=30, blank=True, null=True, verbose_name="Printer Model")
sitename = models.ForeignKey(Site, on_delete=models.SET_NULL, blank=True, null=True, verbose_name="Site")
def __str__(self):
return self.name
URL:
urlpatterns = [
path('', views.SiteView.as_view(), name='site'),
path('report/<sitename>', views.ReportView.as_view(), name='reportview'),
]
Template 1: Site List
<table>
<tr>
<th>Site</th>
<th>Address</th>
</tr>
{% for site in site_list %}
<tr>
<td><a href="{% url 'reportview' site.site_name %}">{{ site.site_name }}</a></td>
<td>{{ site.site_address }}</td>
</tr>
{% endfor %}
</table>
Template 2: Report equipment by site
<table>
<tr>
<th>Device</th>
<th>Serial</th>
</tr>
{% for item in site %}
<tr>
<td>{{ item.name }}</td>
<td>{{ item.serial }}</td>
</tr>
{% endfor %}
</table>
View for template 1: Successful output:
class SiteView(ListView):
model = Site
template_name = "mytestapp/site.html"
View for template 2: Failed
class ReportView(TemplateView):
template_name = "mytestapp/report.html"
def post(self, request):
site = request.POST["site"]
context = super().get_context_data(request)
context["site"] = Workstation.objects.get(sitename=site)
context["site"] = Laptop.objects.get(sitename=site)
context["site"] = Printer.objects.get(sitename=site)
return context
I am trying to list workstation, laptop and printer in single table filter by a site. Sample output: