如何更有效地编写此Django HTML模板?

阿拉法特汗

我有一个Django应用,用户可以在其中创建列表并添加待办事项。我想添加一个新页面,其中将显示有到期日的待办事项。就像在页面中一样,将有一个名为“较早”的部分,该部分是具有截止日期的功能任务。其他部分是“今天”,“明天”和“以后”。现在让我展示我的视图类和HTML代码并解释我的问题。

基于类的视图来处理此页面:

class ToDoNextUpView(LoginRequiredMixin, ListView):
    model = ToDo
    template_name = "ToDo/next_up.html"
    ordering = ["-due_date"]
    context_object_name = "todos"

    def get_queryset(self):
        query_set = []
        for todo in ToDo.objects.filter(creator=self.request.user, is_checked=False):
            if todo.due_date is not None:
                query_set.append(todo)

        return query_set

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        today = datetime.datetime.now(datetime.timezone.utc)
        tomorrow = today + datetime.timedelta(days=1)

        todos_earlier = []
        todos_today = []
        todos_tomorrow = []
        todos_later = []

        for todo in ToDo.objects.filter(creator=self.request.user, is_checked=False):
            if todo.due_date is not None:
                if todo.due_date.day == today.day and todo.due_date.month == today.month and todo.due_date.year == today.year:
                    todos_today.append(todo)
                elif todo.due_date.day == tomorrow.day and todo.due_date.month == tomorrow.month and todo.due_date.year == tomorrow.year:
                    todos_tomorrow.append(todo)
                elif todo.due_date < today:
                    todos_earlier.append(todo)
                elif todo.due_date > tomorrow:
                    todos_later.append(todo)

        context["todos_earlier"] = todos_earlier
        context["todos_today"] = todos_today
        context["todos_tomorrow"] = todos_tomorrow
        context["todos_later"] = todos_later


        return context

这是 next_up.html

{% extends "ToDo/base.html" %}
{% block content %}
{% load crispy_forms_tags %}

{% if todos_earlier %}
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">Earlier</h1></center>
    <br>
    {% for todo in todos_earlier %}
        <article class="content-section">
            <div class="media-body full-content">
                <h2>
                    <button onclick="location.href='{% url 'todo-check' todo.pk %}'" class="btn btn-outline-success">
                        <i class="fa fa-check"></i>
                    </button> 
                    <a style="overflow: hidden; text-overflow: ellipsis;" href="{% url 'todo-detailed' todo.title todo.pk %}" class="article-title">  {{ todo.title }}</a> 
                    <button data-toggle="modal" data-target="#exampleModalCenter{{ todo.pk }}" style="float: right;" class="btn btn-outline-danger">
                        <i class="fas fa-trash"></i>
                    </button>
                    {% if not todo.important %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-secondary">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                        {% else %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-warning">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                    {% endif %}
                </h2>
                <h3 style="color:red">
                    Was due on: {{ todo.due_date|date:"F d" }}
                </h3>
            </div>
        </article>
    {% endfor %}
{% endif %}

{% if todos_today %}
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">For Today</h1></center>
    <br>
    {% for todo in todos_today %}
        <article class="content-section">
            <div class="media-body full-content">
                <h2>
                    <button onclick="location.href='{% url 'todo-check' todo.pk %}'" class="btn btn-outline-success">
                        <i class="fa fa-check"></i>
                    </button> 
                    <a style="overflow: hidden; text-overflow: ellipsis;" href="{% url 'todo-detailed' todo.title todo.pk %}" class="article-title">  {{ todo.title }}</a> 
                    <button data-toggle="modal" data-target="#exampleModalCenter{{ todo.pk }}" style="float: right;" class="btn btn-outline-danger">
                        <i class="fas fa-trash"></i>
                    </button>
                    {% if not todo.important %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-secondary">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                        {% else %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-warning">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                    {% endif %}
                </h2>
                <h3 style="color:blue">
                    Due today
                </h3>
            </div>
        </article>
    {% endfor %}
{% endif %}

{% if todos_tomorrow %}
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">For Tomorrow</h1></center>
    <br>
    {% for todo in todos_tomorrow %}
        <article class="content-section">
            <div class="media-body full-content">
                <h2>
                    <button onclick="location.href='{% url 'todo-check' todo.pk %}'" class="btn btn-outline-success">
                        <i class="fa fa-check"></i>
                    </button> 
                    <a style="overflow: hidden; text-overflow: ellipsis;" href="{% url 'todo-detailed' todo.title todo.pk %}" class="article-title">  {{ todo.title }}</a> 
                    <button data-toggle="modal" data-target="#exampleModalCenter{{ todo.pk }}" style="float: right;" class="btn btn-outline-danger">
                        <i class="fas fa-trash"></i>
                    </button>
                    {% if not todo.important %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-secondary">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                        {% else %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-warning">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                    {% endif %}
                </h2>
                <h3 style="color:green">
                    Due tomorrow
                </h3>
            </div>
        </article>
    {% endfor %}
{% endif %}

{% if todos_later %}
    <br>
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">Coming up later</h1></center>
    <br>
    {% for todo in todos_later %}
        <article class="content-section">
            <div class="media-body full-content">
                <h2>
                    <button onclick="location.href='{% url 'todo-check' todo.pk %}'" class="btn btn-outline-success">
                        <i class="fa fa-check"></i>
                    </button> 
                    <a style="overflow: hidden; text-overflow: ellipsis;" href="{% url 'todo-detailed' todo.title todo.pk %}" class="article-title">  {{ todo.title }}</a> 
                    <button data-toggle="modal" data-target="#exampleModalCenter{{ todo.pk }}" style="float: right;" class="btn btn-outline-danger">
                        <i class="fas fa-trash"></i>
                    </button>
                    {% if not todo.important %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-secondary">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                        {% else %}
                        <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                            type="submit" class="btn btn-warning">
                            <span aria-hidden="true"><i class="fas fa-star"></i></span>
                        </button>
                    {% endif %}
                </h2>
                <h3 style="color:seagreen">
                    Due on: {{ todo.due_date|date:"F d" }}
                </h3>
            </div>
        </article>
    {% endfor %}
{% endif %}

{% endblock content %}

如您所见,我为这些部分的每一部分编写了几乎完全相同的HTML代码。尽管从技术上讲这是可行的,但我永远无法独自生活。我必须写四遍那些复选按钮,四遍写星形按钮,其他四遍。不好

另外,在我的视图中,我做了一些草率的编码,检查了日期,月份和年份,这些似乎不太正确。但是我知道仅检查日期是不好的,因为同一天可能是不同的月份。需要明确的是,这些是DateTime对象,因此我不能等于它们,因为精确的秒数将不匹配。但是我想这还是可以的。我的主要问题是,我应该在HTML和Python代码中带来哪些变化,以便可以在单个for循环(或嵌套循环,但我不想四次重复四个相同的代码)中显示所有部分。

谢谢。

伯纳多·杜阿尔特

我相信您应该为每个待办事项部分使用Django的内置模板标记include,并为其中的每个内容创建一个文件,以使用共享逻辑扩展一个基本的通用文件。

下面我展示了一个可以使用的示例,现在不用编写4次代码即可编辑 todo_generic.html

next_up.html

{% extends "ToDo/base.html" %}
{% block content %}
{% load crispy_forms_tags %}

{% if todos_earlier %}
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">Earlier</h1></center>
    <br>
    {% include "todos_earlier.html" with todos=todos_earlier %}
{% endif %}

{% if todos_today %}
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">For Today</h1></center>
    <br>
    {% include "todos_today.html" with todos=todos_today %}
{% endif %}

{% if todos_tomorrow %}
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">For Tomorrow</h1></center>
    <br>
    {% include "todos_tomorrow.html" with todos=todos_tomorrow %}
{% endif %}

{% if todos_later %}
    <br>
    <center><h1 style="font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 10ch; background-color: royalblue;">Coming up later</h1></center>
    <br>
    {% include "todos_later.html" with todos=todos_later %}
{% endif %}

{% endblock content %}

todo_generic.html

{% for todo in todos %}
    <article class="content-section">
        <div class="media-body full-content">
            <h2>
                <button onclick="location.href='{% url 'todo-check' todo.pk %}'" class="btn btn-outline-success">
                    <i class="fa fa-check"></i>
                </button> 
                <a style="overflow: hidden; text-overflow: ellipsis;" href="{% url 'todo-detailed' todo.title todo.pk %}" class="article-title">  {{ todo.title }}</a> 
                <button data-toggle="modal" data-target="#exampleModalCenter{{ todo.pk }}" style="float: right;" class="btn btn-outline-danger">
                    <i class="fas fa-trash"></i>
                </button>
                {% if not todo.important %}
                    <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                        type="submit" class="btn btn-secondary">
                        <span aria-hidden="true"><i class="fas fa-star"></i></span>
                    </button>
                {% else %}
                    <button style="float: right; margin-right: 5px;" onclick="location.href='{% url 'todo-toggle-important' todo.pk %}'"
                        type="submit" class="btn btn-warning">
                        <span aria-hidden="true"><i class="fas fa-star"></i></span>
                    </button>
                {% endif %}
            </h2>
            {% block due %}{% endblock %}
        </div>
    </article>
{% endfor %}

todos_earlier.html

{% extends "todo_generic.html" %}
{% block due %}
<h3 style="color:red">
    Was due on: {{ todo.due_date|date:"F d" }}
</h3>
{% endblock due %}

todos_today.html

{% extends "todo_generic.html" %}
{% block due %}
<h3 style="color:blue">
    Due today
</h3>
{% endblock due %}

todos_tomorrow.html

{% extends "todo_generic.html" %}
{% block due %}
<h3 style="color:green">
    Due tomorrow
</h3>
{% endblock due %}

todos_later.html

{% extends "todo_generic.html" %}
{% block due %}
<h3 style="color:seagreen">
    Due on: {{ todo.due_date|date:"F d" }}
</h3>
{% endblock due %}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何更有效地编写此CSS?

来自分类Dev

如何更有效地编写此CSS?

来自分类Dev

如何更有效地生成大量HTML

来自分类Dev

比通过HTML更有效地将Power BI与Exact Online连接

来自分类Dev

更有效地编写if语句

来自分类Dev

如何有效地测试此Django模型?

来自分类Dev

如何有效地将大型数据集与HTML页面分开

来自分类Dev

Javascript:将鼠标悬停在问题上,如何有效地构造html?

来自分类Dev

如何使用 Javascript 有效地确定 HTML 文档中同级元素的相对顺序

来自分类Dev

如何使用for循环更有效地编写pandas命令

来自分类Dev

我如何更有效地在这段代码中编写 jQuery?

来自分类Dev

我如何更有效地编写以下代码?

来自分类Dev

如何使此PHP代码更有效地运行?

来自分类Dev

如何使此代码更有效地应对编码挑战?

来自分类Dev

如何更有效地使用find命令?

来自分类Dev

如何更有效地找到闭环

来自分类Dev

如何更有效地解码RSA加密?

来自分类Dev

如何更有效地使用find命令?

来自分类Dev

如何更有效地重写

来自分类Dev

如何更有效地呈现Mathjax内容?

来自分类Dev

如何更有效地找到闭环

来自分类Dev

如何更有效地计算滚动比

来自分类Dev

如何在Django模板中优雅地编写此代码?

来自分类Dev

如何在Ruby中更有效地编写远程/本地服务器文件操作方法?

来自分类Dev

如何才能更有效地编写代码,以与不同类型的JavaFX GUI组件一起使用?

来自分类Dev

我可以在R中更有效地编写方程式吗?

来自分类Dev

比{{}}更有效的在模板中调用Angular方法的方法?

来自分类Dev

PHPExcel-有效地从数据库转换HTML标记

来自分类Dev

使用Python有效地请求和处理多个HTML文件

Related 相关文章

热门标签

归档