Reputation: 17
I have defined models and views. I would like to display Project existing from database. However script is not displaying any content. Where's the problem? Please take a look into definition of model, views and html template of file trying to display projects from database with use of for loop.
models.py
from django.db import models
from bifrost.models import CustomUser
# Create your models here.
# Model Projektu
class Project(models.Model):
PROJECT_TYPE = (
('SCR', 'Scrum'),
('KAN', 'Kanban'),
)
project_key = models.CharField(max_length=8, primary_key=True)
project_name = models.CharField(max_length=160)
project_type = models.CharField(max_length=10, choices=PROJECT_TYPE, null=True)
date_created = models.DateField(null=True)
# Definicja nazwy modelu w Adminie Django
def __str__(self):
return self.project_name
views.py
from django.views.generic import ListView
from django.shortcuts import render
from .models import Project
# Create your views here.
class ProjectListView(ListView):
model = Project
template_name = 'project-list.html'
contect_object_name = 'projects_list'
def projectslist(request):
projects = Project.objects.all()
return render(request, 'project_list.html', {'projects': projects})
project-list.html template
{% extends 'base.html' %}
<h1 class="h3 mb-2 text-gray-800">{% block title %}Projects{% endblock title %}</h1>
{% block content %}
<!-- DataTales Example -->
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-primary">List of Projects</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Project Key</th>
<th>Name</th>
<th>Type</th>
<th>Created</th>
</tr>
</thead>
<!-- <tfoot>
<tr>
<th>Project Key</th>
<th>Name</th>
<th>Type</th>
<th>Created</th>
</tr>
</tfoot> -->
<tbody>
{% for project in projects_list %}
<tr>
<td>{{ project.project_key }}</td>
<td>{{ project.project_name }}</td>
<td>{{ project.project_type }}</td>
<td>{{ project.date_created }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock content %}
Debugger is not showing any issues. Pylint only show small suggestions, no errors as well.
Upvotes: 0
Views: 1196
Reputation: 161
{% for project in projects_list %}
Change this row in your template because you passed projects as your key in context and you are using projects_list.
Try {% for project in projects %}
And it should work.
Upvotes: 1
Reputation: 1340
From your views file you are passing projects as a parameter like: return render(request, 'project_list.html', {'projects': projects})
, and in templates files you are accessing it with projects_list, which will not return anything.
In you templates file replace:
{% for project in projects_list %}
with:
{% for project in projects %}
It will work.
Upvotes: 1