Django:如何链接到特定用户? [英] Django: how to link to specific user?
问题描述
我正在尝试创建一个具有待办事项功能的网站!到目前为止,我已经对其进行了编程,因此它可以与一个用户完美配合.但是,当我使用其他用户登录时,仍然可以查看待办事项列表,但我不希望这样做.
I am trying to create a website with a To-do function! So far, I've programmed it so it works perfectly with one user. However, when I log in with a different user, I can still view my to-do list, and I don't want this.
现在,如果我可以使这一特定功能对很多人都可以使用,我将非常高兴.因此此人登录到自己的帐户,创建了自己的列表,/我登录以创建自己的列表.
Now, I'd be very happy if I can make this specific feature useable to lots of people. so this person logs into his account, create his own list / I log in to mine and create my own one.
我搜索了太多的YouTube教程和文章,但没有一个帮助到您:(如果在您这样的编码专家的大力协助下,我可以在这里解决这个问题,那就太好了!
I've searched for so many youtube tutorials and articles, none of them helped :( It'll be awesome if I can solve this here, with lots of help from coding experts like you!
这是与待办事项功能相关的代码
Here's my code that's related to To-do feature
Views.py
def ToDo(request):
todos = TodoList.objects.all()
categories = Category.objects.all()
if request.method == "POST":
if "taskAdd" in request.POST:
title = request.POST["description"]
date = str(request.POST["date"])
category = request.POST["category_select"]
content = title + " -- " + date + " " + category
Todo = TodoList(title=title, content=content, due_date=date,
category=Category.objects.get(name=category))
Todo.save()
return redirect("/to_do")
if "taskDelete" in request.POST:
print(request.POST)
checkedlist = request.POST.getlist('checkedbox')
for todo_id in checkedlist:
todo = TodoList.objects.get(id=int(todo_id))
todo.delete()
return render(request, 'Todolist.html', {"todos": todos, "categories":categories})
models.py
models.py
class TodoList(models.Model):
title = models.CharField(max_length=250)
content = models.TextField(blank=True)
created = models.DateField(default=timezone.now().strftime("%Y-%m-%d"))
due_date = models.DateField(default=timezone.now().strftime("%Y-%m-%d"))
category = models.ForeignKey(Category, on_delete=models.CASCADE)
class Meta:
ordering = ["-created"]
def __str__(self):
return self.title
Todo.html
Todo.html
<div django-app="TaskManager">
<div class="container">
<div class="content">
<h2 style="text-align: center">Tasks are listed here!</h2>
<p class="tagline">Jayden's To-Do system</p>
<form action="" method="post">
{% csrf_token %}
<div class="inputContainer">
<label for="category">What should I do??</label>
<input type="text" id="description" class="taskName" placeholder="Task Description" name="description" required>
</div>
<div class="inputContainer half last">
<i class="fa fa-caret-down selectArrow"></i>
<label for="category">Course?</label>
<select id="category" class="taskCategory" name="category_select">
<option class="disabled" value="">Choose the course</option>
{% for category in categories %}
<option class="" value="{{ category.name }}" name="{{ category.name }}">{{ category.name }}</option>
{% endfor %}
</select>
</div>
<div class="inputContainer half last right">
<label for="dueDate">Until when? (Due Date)</label>
<input type="date" id="dueDate" class="taskDate" name="date">
</div>
<div class="row">
<button class="taskAdd" name="taskAdd" type="submit"><i class="fa fa-plus icon"></i>Add task</button>
<button class="taskDelete" name="taskDelete" formnovalidate="" type="submit" onclick="$('input#sublist').click();"><i class="fa fa-trash-o icon"></i>Delete Tasks</button>
</div>
<ul class="taskList">
{% for todo in todos %}
<li class="taskItem">
<input type="checkbox" class="taskCheckbox" name="checkedbox" id="{{ todo.id }}" value="{{ todo.id }}">
<label for="{{ todo.id }}"><span class="complete-">{{ todo.title }}</span></label>
<span class="category-{{ todo.category }}">{{ todo.category }}</span>
<strong class="taskDate"><i class="fa fa-calendar"></i>{{ todo.created }} - {{ todo.due_date }}</strong>
</li>
{% endfor %}
</ul>
</form>
</div>
有人可以帮我具体说明一下用户吗?谢谢!!
Could someone please help me to make this specific to users? Thank you!!
推荐答案
首先,您的TODO模型需要与用户关联
First your TODO model needs to associate to a user
from django.conf import settings
class TodoList(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=250)
创建待办事项时,将其链接到用户
When you create the todo, link it to the user
def ToDo(request):
...
Todo = TodoList(user=request.user, title=title, content=content, due_date=date,
category=Category.objects.get(name=category))
Todo.save()
...
检索列表时,需要按用户进行过滤
When you retrieve the list, you need to filter by the user
def ToDo(request):
todos = ToDo.objects.filter(user=request.user)
...
这篇关于Django:如何链接到特定用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!