Skip to content Skip to sidebar Skip to footer

Django: Keep Each Users Data Separate

I am trying to workout how / the best, most secure way to keep a user's data separate within a django site that I need to write. Here is an example of what I need to do... example

Solution 1:

One approach is to filter the ToDo items by the currently logged in user:

from django.contrib.auth.decorators import login_required
from django.shortcuts import render

from your_app.models import ToDo

@login_requireddeftodos_for_user(request):
    todos = ToDo.objects.filter(user=request.user)
    return render(request, 'todos/index.html', {'todos' : todos})

This locks down the view for authenticated users only, and filtering by the logged in user from the request, another user, even if logged in, can't access another user's ToDo records. Hope that helps you out.

Solution 2:

Make url like www.domain.com/username/todo is one way to implement it, but it doesn't guarantee you achieve security.

What you should do keep your user's login information in a session data after user login, and every time you check certain view,

  1. check whether that particular user has right to see this view.
  2. using user's login info (ID, or username) when querying user's Todo list.

And I guess this link will help you to do your job.

Sessions, Users, and Registration.

Post a Comment for "Django: Keep Each Users Data Separate"