Can’t render any data from model
Here is views.py file
from django.shortcuts import render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy
from django.contrib.auth.views import LoginView
from django.contrib.auth.mixins import LoginRequiredMixin
from . models import Task
class CustomLoginView(LoginView):
template_name = 'base/login.html'
fields = ['title', 'description', 'complete']
redirect_authenticated_user = True
def get_success_url(self):
return reverse_lazy('tasks')
class TaskList(LoginRequiredMixin, ListView):
model = Task
context_object_name = 'tasks'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['color'] = 'red'
return context
class TaskDetail(LoginRequiredMixin, DetailView):
model = Task
context_object_name = 'task'
template_name = 'base/task.html'
class TaskCreate(LoginRequiredMixin, CreateView):
model = Task
fields = ['title', 'description', 'complete']
success_url = reverse_lazy('tasks')
class TaskUpdate(LoginRequiredMixin, UpdateView):
model = Task
fields = ['title', 'description', 'complete']
success_url = reverse_lazy('tasks')
class TaskDelete(LoginRequiredMixin, DeleteView):
model = Task
context_object_name = 'task'
success_url = reverse_lazy('tasks')
models.py
from django.db import models
from django.contrib.auth.models import User
class Task(models.Model):
user = models.ForeignKey(
User, on_delete=models.CASCADE, blank=True, null=True)
title = models.CharField(max_length=200)
description = models.TextField(null=True, blank=True)
complete = models.BooleanField(default=False)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
class Meta:
ordering = ['complete']
And template task_list.html
{% if request.user.is_authenticated %}
<p> {{ request.user }}</p>
#</a href="{% url 'logout' %}">Logout</a/>new users can only put 2 links in a post.
{% else %}
#<a href="{% url 'login' %}">Login</a>new users can only put 2 links in a post.
{% endif %}
<hr>
<h1>My to do list {{color}} </h1>
<td><a href="{% url 'task-create' %}">Add</a> </td>
<table>
<tr>
<th>Items</th>
<th></th>
</tr>
{%for task in tasks%}
<tr>
<td>{{task.title}}</td>
<td><a href="{% url 'task' task.id %}">View</a> </td>
<td><a href="{% url 'task-update' task.id %}">Edit</a> </td>
<td><a href="{% url 'task-delete' task.id %}">Del</a> </td>
{%empty%}
<h3>No items in a list </h3>
</tr>
{%endfor%}
can’t render specific data from user ,any help will be appreciated