I am new to Django and I'm making a todo app. It's almost done, but I can't see the user-entered data. I have a program that shows TODO tasks, and I can log in in as any user and see anyone's tasks, but I want each user to have a different tasks list as a personal todo app.

Here's some of my code:

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User


class To_DO_Fun(models.Model):
    Task_Name = models.CharField(max_length=20, default='')
    Task_Text = models.TextField(max_length=450, default='')
    Task_Done = models.BooleanField(default=False)

    def __str__(self):
        return self.Task_Name + '  --- is ---  ' + str(self.Task_Done)


class user_TODO(models.Model):
    User_Name = models.ForeignKey(To_DO_Fun , User, on_delete=models.CASCADE)
from __future__ import unicode_literals
from django.shortcuts import render, redirect
from .models import To_DO_Fun, user_TODO #Class Name
from .forms import Form_todo, Form_Task #Form Name
from django.contrib import messages
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required



def HomeFun(request): #To view contents of the home page
    return render(request, "ToDo_Files/HomePage.html")


@login_required(login_url='Login-Page')
def Display_Page(request, User_Name_id):

    if request.method == "POST":

        form = Form_todo(request.POST or None)

        if form.is_valid():
            all_items = To_DO_Fun.objects.get(pk=User_Name_id)
            form.save()
            messages.success(request, ("Good Luck"))
            return render(request , 'ToDo_Files/Display_Page.html' , {'all_items': all_items} )

    else:
        form = Form_todo(request.POST or None)
        all_items = To_DO_Fun.objects.get(pk=User_Name_id)
        return render(request,'ToDo_Files/Display_Page.html',{'all_items': all_items} )




@login_required(login_url='Login-Page')
def remove(request, To_DO_Fun_id):
    item = To_DO_Fun.objects.get(pk=To_DO_Fun_id)
    item.delete()
    messages.success(request, ('Congratulations!!! You finished your task'))
    return redirect('Display-Page')




@login_required(login_url='Login-Page')
def data(request, To_DO_Fun_id):

        if request.method == "POST":
            item_all = To_DO_Fun.objects.get(pk=To_DO_Fun_id)
            form = Form_Task(request.POST, instance= item_all)

            if form.is_valid():
                form.save()
                urlstr = 'ToDo_Files/details/'
                messages.success(request, ("Item Updated"))
                return redirect('Display-Page')

        else:
            item_all = To_DO_Fun.objects.get(pk=To_DO_Fun_id)
            return render(request, 'ToDo_Files/details.html', {'item_all' : item_all})

        return redirect('Display-Page')



@login_required(login_url='Login-Page')
def Done_status(request, To_DO_Fun_id):
    item = To_DO_Fun.objects.get(pk=To_DO_Fun_id)
    item.Task_Done = True
    item.save()
    return redirect('Display-Page')

@login_required
def Pen_status(request, To_DO_Fun_id):
    item = To_DO_Fun.objects.get(pk=To_DO_Fun_id)
    item.Task_Done = False
    item.save()
    return redirect('Display-Page')







def Reg_Fun(request):
    if request.method == "POST":
        UserReg_Form = UserCreationForm(request.POST)

        if UserReg_Form.is_valid():
            UserReg_Form.save()
            messages.success(request, ("Welcome"))
            return redirect('Login-Page')
    else:
        UserReg_Form = UserCreationForm(request.POST)
        return render(request, "ToDo_Files/reg.html", {'UserReg_Form':UserReg_Form})

        return redirect('Login-Page')

1 Answers

0
robert lee On Best Solutions

Add a many-to-one-relationship from your tasks model to the User model.

in your models.py

class To_DO_Fun(models.Model):
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    Task_Name = models.CharField(max_length=20, default='')
    Task_Text = models.TextField(max_length=450, default='')
    Task_Done = models.BooleanField(default=False)

then in your views.py you can retrieve all the tasks that belong to the user that is currently logged in using a filter.

tasks = To_DO_Fun.objects.filter(owner=request.user)
context = {'tasks': tasks}
return(request, "yourpage.html", context)

I suggest you take a look at Django's coding style following the coding style will improve your code consistency and readability.