two models into one form into view

Hey everyone,

The idea is to have this:


This will be kind of the demo for the Todo list. To create a list you have to click on the Create List button. That is now the idea.

I created the 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']


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


class TodoListUpdate(UpdateView):
	model = TodoList
	fields = '__all__'
	tempate_name_suffix = '_update_form'


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

project/urls.py:

from django.contrib import admin
from django.urls import path

from ToDoro_app import views

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", views.home, name='home'),
    path("create/<int:pk>", TodoListUpdateView.as_view(), name='create_update_form'),
    path("edit/", views.edit, name='edit'),
    path("complete/<int:todo_id>", views.complete, name='complete'),
    path("incomplete/<int:todo_id>", views.incomplete, name='incomplete'),
]

But I get the error in terminal:

path("create/<int:pk>", TodoListUpdateView.as_view(), name='create_update_form'),
NameError: name 'TodoListUpdateView' is not defined

Ok, I have not defined a function in views.py for TodoListUpdateView but I thought that the TemplateView will do the trick. What do I missing?

Thanks
Doro

You need to import the view in the urls.py file.

LOL that seems easy :rofl:

Thank you