I am creating a web application that allows a user to create a todolist where they can add, delete, and edit todo list items (todos). I am using the django framework to do so and I have gotten it to work for the most part. So far, I have implemented adding, editing, and deleting todos from a todolist, I have created a superuser, and I am able to log in to the site using my superuser.
However, I want to let each user have their own unique todolist. I don't want users to see each other's todolists. I am new to python and django and I am not sure how to go about doing this. I have created a test user using the admin site and when I log in using this test user, I am taken to the page with the same (and only) todo list. I have not yet implemented a registration page for new users and I want to be able to link users to their own todolists before I do that. This is what I have so far:
// models.py
class Todo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
text = models.TextField()
created_at = models.DateTimeField(default=datetime.now, blank=True)
def __str__(self):
return self.title
@receiver(post_save, sender=User)
def create_user_todos(sender, instance, created, **kwargs):
if created:
Todo.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_todos(sender, instance, **kwargs):
instance.todo.save()
// views.py
def index(request, user_id):
user = User.objects.get(pk=user_id)
todos = user.todo.objects.all()[:10]
context = {
'todos': todos # to pass them to index.html
}
return render(request, 'index.html', context)
def details(request, id, user_id):
user = User.objects.get(pk=user_id)
todo = user.todo.objects.get(id=id)
context = {
'todo': todo
}
return render(request, 'details.html', context)
def add(request, user_id):
user = User.objects.get(pk=user_id)
if(request.method == 'POST'):
title = request.POST['title']
text = request.POST['text']
todo = user.todo(title=title, text=text)
todo.save()
return redirect('/todos')
else:
return render(request, 'add.html')
def edit(request, id, user_id):
user = User.objects.get(pk=user_id)
todo = user.todo.objects.get(id=id)
context = {
'todo': todo
}
if(request.method == 'POST'):
title = request.POST['title']
text = request.POST['text']
if len(str(title)) != 0:
todo.title = title
if len(str(text)) != 0:
todo.text = text
todo.save()
return redirect('/todos')
else:
return render(request, 'edit.html', context)
def delete(request, id, user_id):
user = User.objects.get(pk=user_id)
todo = user.todo.objects.get(id=id)
context = {
'todo': todo
}
if(request.method == 'POST'):
Todo.delete(todo)
return redirect('/todos')
else:
return render(request, 'delete.html', context)
I was following a tutorial beforehand where I added the extra parameter user_id to all my functions in views.py. It is giving me an error now but if I don't include the paramter and change my implementation of the functions to be
todo = Todo.objects.get(id=id)
then everything works.
I realize my question is vague and I have tried searching for solutions but everything I have read assumes I have some previous in depth knowledge of django (which I don't). Please lead me in the right direction :)