So I am following this guide: https://www.youtube.com/watch?v=_sWgionzDoM and am at around 55 mins so far
I have the login stuff all working however I am unable to get the dashboard to show my data from the database. The table does show on the admin page with the data however when I go to my dashboard for the user it does not show.
Here is the code of my dashboard.
{% extends 'page.html' %}
{% block content %}
{% if messages %}
<div class="row mt-3">
{% for message in messages %}
{% if message.tags == 'error' %}
<div class="col-md-10 col-12 mx-auto alert alert-danger">
{{ message }}
</div>
{% else %}
<div class="col-md-10 col-12 mx-auto alert alert-success">
{{ message }}
</div>
{% endif %}
{% endfor %}
</div>
{% endif %}
<div class="row">
<div class="col-md-10 col-12 mx-auto mt-5">
<div class="d-flex justify-content-end">
<a href="{% url 'main' %}" class="btn btn-primary">+</a>
</div>
<table class="table table-hover table-striped">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Qty</th>
<th scope="col">Category</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{% if items|length == 0 %}
<tr>
<th scope="row">-</th>
<td>no data</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td></td>
</tr>
{% endif %}
{% for item in items %}
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.name }}</td>
{% if item.id in low_inventory_ids %}
<td class="text-danger">{{ item.quantity }}</td>
{% else %}
<td class="text-success">{{ item.quantity }}</td>
{% endif %}
<td>{{ item.category.name }}</td>
<td><a href="{% url 'edit-item' item.id %}" class="btn btn-outline-secondary">Edit</a></td>
<td><a href="{% url 'delete-item' item.id %}" class="btn btn-secondary">Delete</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock content %}
Here is the code of my model
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class InventoryItem(models.Model):
name = models.CharField(max_length=200)
quantity = models.IntegerField()
category = models.ForeignKey("Category", on_delete=models.SET_NULL, blank=True, null=True)
date_created = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE,)
#related_name='accounts_inventoryitems
def __str__(self):
return self.name
class Category(models.Model):
name = models.CharField(max_length=200)
class Meta:
verbose_name_plural = 'categories'
def __str__(self):
return self.name`
and here is my views.py file
class Dashboard(View):
def get(self, request):
items = InventoryItem.objects.filter(user=self.request.user.id).order_by('id')
return render(request, 'dashboard.html', {'items': items})
Im really not sure at which point it is no longer sending the data. I did a debug and this shows that the database is populated with correct values.
I hope you can help!