【问题标题】:HTML Calendar Appearing Under Footer出现在页脚下的 HTML 日历
【发布时间】:2022-11-25 05:02:17
【问题描述】:

我为我的 django 应用程序创建了一个 HTML 日历。但是,当我将它添加到我的其中一个模板时,它会将它添加到我的页脚下方。我不明白为什么会这样。

{% extends "bf_app/app_bases/app_base.html" %}

{% block main %}
    {% include "bf_app/overviews/overview_nav.html" %}
    <div class="flex justify-between mx-10">
        <a href="{% url 'calendar_overview_context' previous_year previous_month %}">< {{ previous_month_name }}</a>
        <a href="{% url 'calendar_overview_context' next_year next_month %}">{{ next_month_name }} ></a>
    </div>
    <div class="grid grid-cols-1 md:grid-cols-3 px-4">
        <table>
            <thead>
            <tr>
                <th class="text-left">Transaction</th>
                <th class="text-left">Amount</th>
                <th class="text-left">Date</th>
            </tr>
            </thead>
            {% for transaction, tally in monthly_budget.items %}
                <tr>
                    <td>{{ transaction }}</td>
                    <td class="{% if tally|last == "IN" %}text-green-700{% else %}text-red-700{% endif %}">
                        {{ tally|first|floatformat:2 }}
                    </td>
                    <td>{{ transaction.next_date|date:"D, d M, Y" }}</td>
                </tr>
            {% endfor %}
        </table>
    </div>
    <div>
        {{ calendar }}
    </div>

{% endblock %}

我几乎遵循了本教程: https://www.huiwenteo.com/normal/2018/07/24/django-calendar.html

有什么我想念的吗?据我了解,这应该像我创建的其他所有内容一样位于页脚之上。

编辑:

这似乎是导致问题的“mark_safe”模块。我试过使用{{ calendar|safe }},这也会产生同样的问题。

带保险箱

没有保险箱

【问题讨论】:

  • 您将页脚放在"bf_app/app_bases/app_base.html" 的什么位置,前提是

标签: python html django django-templates


【解决方案1】:

我有同样的问题,使用 huiwenteo 日历教程。

这种奇怪的行为正在制作 Calendar 类,更具体地说是 formatmonth 方法。因为它返回日历表,没有关闭 html 标签。因此,在您的 cal/utils.py 文件中,您应该在返回日历之前将 cal += f'</table> ' 添加到 formatmonth 方法。

这是对我有用的例子。

def formatmonth(self, ....):
        events = Event.objects.filter(....)

        cal = f'<table border="0" cellpadding="0" cellspacing="0" class="calendar">
'
        cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}
'
        cal += f'{self.formatweekheader()}
'
        for week in self.monthdays2calendar(self.year, self.month):
            cal += f'{self.formatweek(week, events)}
'
        cal += f'</table>
'
        return cal

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多