我正在处理一个项目,我想在网页上显示日期。
下面是我的www.example.com中的函数views.py
def date(request):
template = loader.get_template('app/index.html')
current_date = datetime.datetime.now()
format_date = current_date.strftime('%m-%d-%Y')
context = {'date': format_date}
# log format_date
logger.info(format_date)
return HttpResponse(template.render(context, request))
下面是index.html中的相关html
<!-- templates/home.html -->
{% extends 'base.html' %}
<!-- Date -->
{% block date %}
<p class="datedisplay">Today is {{ date }}</p>
{% endblock %}
下面是来自base.html的相关html
<body>
<div class="navbar">
{% block header %}
{% endblock %}
{% block date %}
{% endblock %}
</div>
<main>
<section class="login">
{% block content %}
{% endblock %}
</section>
<section class="bio">
{% block brief %}
{% endblock %}
</section>
</main>
</body>
以下是来自app/ www.example.com的URL模式urls.py
urlpatterns = [
path('', views.index, name='index'),
path('', views.date, name='date')
]
我已经设置了日志记录,可以确认格式化的日期被正确返回。
我已经尝试了至少6个关于如何将变量从www.example.com显示views.py到网页的来源。我不确定如果我不试图将用户重定向到另一个页面,我是否需要弄乱URL。我所希望的是日期成为打开网页时显示的变量。尽管如此,我还是尝试编辑urls.py与my/app/index.html相关的www.example.com。
我怀疑问题出在index.html和base.html之间的重定向上。在index.html中,我设置了实际的文本,而base.html用于正确地安排它。
1条答案
按热度按时间cu6pst1q1#
你需要在models.py中定义date属性。像这样做:
除非您在模型中定义它,否则您的视图将无法执行它。