I am creating a very simple to-do list application. Each user should have an associated to-do list page with basic CRUD functionality. That means User A should have different data than User B.
As of right now there is no distinction about who owns the to-do list. Anyone who is logged in can add, remove, display, delete tasks.
I have a gut feeling that I might need an extra something in my model and my template. I should mention that I use Pinax 0.9a2. If it does what I need it to do, I would prefer to use that solution instead.
Here’s my models.py
class Task(models.Model):
name = models.CharField(max_length=100)
added_at = models.DateTimeField(auto_now_add=True)
last_update = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.name
Here’s one of my forms in views.py
def task_create(request):
return create_object(request,
model=Task,
template_name='task_create.html',
post_save_redirect=reverse("todo_list")
)
Here’s 2 of my templates:
To-Do Create
<form action="" method="post">{% csrf_token %}
{{ form.name }}
<button type="submit" class="btn primary">Create →</button>
</form>
To-Do List
{% if task_list %}
<p><a href="{% url task_create %}">Create a task</a></p>
<ul>
{% for task in task_list %}
<li><a href="{% url task_detail task.id %}">{{ task.name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No tasks to display, click to <a href="{% url task_create %}">create new</a>.</p>
{% endif %}
So you just want to add access control to it?
ForeignKeytoauth.Userto your ToDos modelcommit=Falseto your form’ssave(), set up user for retrieved object and save it manuallyCode:
Also, add filtering by
request.userin list view and I’d recommend@login_requireddecorator to avoid adding tasks by non-authorized users.