如何按Django / boostrap中的类别过滤任务?

时间:2020-07-13 17:15:48

标签: python django

我一直在遵循本教程:https://medium.com/fbdevclagos/how-to-build-a-todo-app-with-django-17afdc4a8f8c创建待办事项列表。

我想按类别显示每个任务。通常,如果我只想显示一些布尔模型,则在视图中使用过滤器。由于类别最初并未定义,而是一个字符,因此无论名称如何,如何根据类别过滤任务。

(我知道taskDelete函数无法正常工作并返回错误)

models.py

class Category(models.Model): # The Category table name that inherits models.Model
    name = models.CharField(max_length=100) #Like a varchar
    class Meta:
        verbose_name = ("Category")
        verbose_name_plural = ("Categories")
    def __str__(self):
        return self.name #name to be shown when called

class TodoList(models.Model): #Todolist able name that inherits models.Model
    title = models.CharField(max_length=250) # a varchar
    content = models.TextField(blank=True) # a text field 
    comment = models.TextField(blank=True) # a text field
    created = models.DateField(default=timezone.now().strftime("%Y-%m-%d")) # a date
    due_date = models.DateField(default=timezone.now().strftime("%Y-%m-%d")) # a date
    category = models.ForeignKey(Category, on_delete=models.CASCADE, default="general") # a foreignkey
    complete = models.BooleanField(default=False)
    class Meta:
        ordering = ["-created"] #ordering by the created field
    def __str__(self):
        return self.title #name to be shown when called

views.py

def check_list(request):
    todos = TodoList.objects.all() #querying all todos with the object manager
    categories = Category.objects.all() #getting all categories with object manager
    if request.method == "POST": #checking if the request method is a POST
        if "taskAdd" in request.POST: #checking if there is a request to add a todo
            title = request.POST["description"] #title
            date = str(request.POST["date"]) #date
            category = request.POST["category_select"] #category
            content = title + " -- " + date + " " + category #content
            Todo = TodoList(title=title, content=content, due_date=date, category=Category.objects.get(name=category))
            Todo.save() #saving the todo 
            return redirect("/") #reloading the page
        if "taskDelete" in request.POST: #checking if there is a request to delete a todo            
            checkedlist = request.POST["checkedbox"] #checked todos to be deleted
            for todo_id in checkedlist:
                todo = TodoList.objects.get(id=todo_id) #getting todo id
                todo = get_object_or_404(Todo, pk=todo_id)
                todo.delete() #deleting todo
                messages.info(request, "item removed !!!")         
    return render(request, 'marketing/check_list.html',{"todos": todos, "categories":categories})

check_list.html

<div class="container-fluid">
        <div class="row">
            {% for category in categories %}
            <div class="col-sm-3 col-md-3">
                <div class="card">
                    <div class="card-header">
                        <div class="row">
                            <div class="col col-xs-1">
                                <h4 class="card-title">{{ category.name }}</h4>
                            </div>

                        </div>                        
                    </div>
                    <div class="card-body">                       
                            {% for todo in todos %} <!-- django template lang - for loop -->                      
                            <div class="row">    
                                <div class="col">
                                    <input type="checkbox" class="taskCheckbox" name="checkedbox" id="{{ todo.id }}" value="{{ todo.id }}">
                                    <label for="{{ todo.id }}"><span class="complete-">{{ todo.title }}</span></label>
                                </div>
                                <div class="col text-right">                                        
                                    <strong class="taskDate">{{ todo.due_date }}</strong>
                                </div>
                            </div>                           
                            {% endfor %}                                                                 
                    </div>
                </div>  
            </div>
            {% endfor %}         
        </div>
    </div>
</div>

非常感谢

1 个答案:

答案 0 :(得分:0)

由于每个TodoList都有一个category,因此在category循环的模板中,您可以循环访问每个类别的TodoList s

{% for category in categories %}
     some code
     {% for todo in category.TodoList_set.all %}

     {% endfor %}
{% endfor %}