Hello,
I am still working on the ToDoro application. I have decided to not use the inlineformset. Now I am stuck again when displaying the titles with corresponding items on the webpage.
How can I loop through the model and display two or more items with one title? Which condition can I use? I have no idea which condition I should use.
models.py:
class TodoList(models.Model):
title = models.CharField(max_length=150)
def __str__(self):
return self.title
class TodoItem(models.Model):
item = models.CharField(max_length=200)
todolist = models.ForeignKey('TodoList', on_delete=models.CASCADE)
completed = models.BooleanField(default=False)
views.py:
def base(request):
if request.method == "POST":
form_list = TodoListForm(request.POST)
if form_list.is_valid():
form_list.save()
return HttpResponseRedirect('/')
else:
form_list = TodoListForm()
if request.method == "POST":
form_item = TodoItemForm(request.POST)
if form_item.is_valid():
form_item.save()
return HttpResponseRedirect('/')
else:
form_item = TodoItemForm()
# grap all TodoItems from database:
all_todos = TodoItem.objects.all()
# grap all TodoLists (titles) from database:
titles = TodoList.objects.all()
return render(request, 'edit.html',
{
'form_list': form_list,
'form_item': form_item,
'all_todos': all_todos,
'titles': titles
})
edit.html:
<div class="container">
{% for todo in all_todos %}
<h3>{{ todo.todolist }}</h3>
<p>{{ todo.item }}</p>
{% endfor %}
</div>
webpage:
How can I display:
Lorem ipsum dolor
nisi ut aliquip ex ea
incididunt ut labore et
If you need further information please let me know.
Doro