2

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 :)

Zelda
  • 47
  • 1
  • 10
  • You've to have a mapping in your database between the user table and todo list table. That way you'll query the Todo list for a specific user id or whatever foreign key mapping you choose. – bharath Mar 17 '18 at 03:57
  • @bharath what do you mean by mapping? How is this done? – Zelda Mar 17 '18 at 04:14
  • You can read this answer here to know more: https://stackoverflow.com/questions/13732851/mapping-relationship-foreign-key-or-mapping-table – bharath Mar 17 '18 at 05:06
  • If you want to know how to write schema for foreign key relationship, you can see this link here: https://www.w3schools.com/sql/sql_foreignkey.asp – bharath Mar 17 '18 at 05:18
  • From looking at your `views.py`, I can see that you've got something wrong here. If these views are intended for each user who has already logged in, you shouldn't be getting user ids from the URL. To understand Django's auth framework you should have a look at the Django's documentation https://docs.djangoproject.com/en/2.0/topics/auth/. I learned Django from Django's Cookbook, I highly recommend it. Get it from https://code.djangoproject.com/wiki/CookBook – rabbit.aaron Mar 17 '18 at 05:40
  • Also, could you explain your model relation a bit more clearly? How many Todos should a user have? From what I can get out of your code, one user has exactly one Todo, thus your `add` view function doesn't really make sense. – rabbit.aaron Mar 17 '18 at 05:42

0 Answers0