Hey everyone,
I have two issues in my app.
It is a Todo application, on the base.html page there is the form to create the first Todo title and one item. This form is not working or I do not know. I can not find the issue.
And the second issue is that I can not display the items which I created with the Admin panel on the homepage.
base.html:
<div class="container">
<form method="POST">
{% csrf_token %}
<div class="row">
<div class="col-md-6">
<div class="card border-dark bg-light my-3" >
<div class="card-header bg-transparent ">
<div class="input-group">
<input
name="title"
type="text"
class="form-control"
placeholder="ToDoro Create new List">
</div>
</div>
<div class="card-body">
<div class="card" >
<table class="table table-striped">
<tbody>
<tr>
<td>
<i class="far fa-circle"></i>
</td>
<td>
<div class="input-group">
<input
name="items"
type="text"
class="form-control"
placeholder="Add an Item">
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="mt-2">
<button
type="submit"
class="btn btn-primary">
Create List
</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
homepage.html:
<div class="container">
{% for todo in todos %}
<div class="row">
<div class="col-md-6">
<div class="card border-dark bg-light my-3" >
<div class="card-header bg-transparent ">
{{ todo.title }}
</div>
<div class="card-body">
<div class="card" >
<table class="table table-striped">
<tbody>
<tr>
<td>
<i class="far fa-circle"></i>
</td>
{% for item in todo_items %}
<td>
{{ item.items }}
</td>
{% endfor %}
<td>
<a href="{% url 'edit' %}"><i class="far fa-edit"></i></a>
<a href="{% url 'edit' %}"><i class="far fa-trash-alt"></i></a>
</td>
</tr>
</tbody>
</table>
</div>
<div class="mt-2">
<a href="#" class="btn btn-danger"> Delete List</a>
</div>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
models.py:
from django.db import models
class TodoItem(models.Model):
item = models.CharField(max_length=200)
def __str__(self):
return self.item
class TodoList(models.Model):
title = models.CharField(max_length=200)
items = models.ManyToManyField(TodoItem)
completed = models.BooleanField(default=False)
def __str__(self):
return self.title
forms.py:
from django import forms
from .models import TodoList, TodoItem
class TodoListForm(forms.ModelForm):
class Meta:
model = TodoList
fields = ['title', 'items', 'completed']
class TodoItemForm(forms.ModelForm):
class Meta:
model = TodoItem
fields = ['item']
views.py:
from django.shortcuts import render, redirect
from django.contrib import messages
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.base import TemplateView
from .models import TodoList, TodoItem
from .forms import TodoListForm, TodoItemForm
def home(request):
if request.method == 'POST':
form = TodoListForm(request.POST or None)
if form.is_valid():
form.save()
todos = TodoList.objects.all()
todo_items = TodoItem.objects.all()
messages.success(request, ('New Todo List added.'))
return render(request, 'homepage.html', {'todos': todos}, {'todo_items': todo_items})
else:
todos = TodoList.objects.all()
return render(request, 'homepage.html', {'todos': todos})
todos = TodoList.objects.all()
return render(request, 'homepage.html', {'todos': todos})
class TodoListUpdate(UpdateView):
model = TodoList
fields = '__all__'
tempate_name_suffix = '_update_form'
class TodoListCreateView(CreateView):
model = TodoList
fields = '__all__'
class TodoListUpdateView(TemplateView):
template_name = '_update_form.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
todo_list = get_object_or_404(TodoList, pk=self.kwargs['pk'])
context['todo_list'] = todo_list
context['todo_list_form'] = TodoListForm(instance=todo_list)
context['todo_item_form'] = TodoItemForm(instance=todos.todoItem)
return context
I tried already some thinks but I can not make it work. I am lost.
I would appreciate some hints that I can make it work and learn.
Thank you
Doro