在练习使用 Django,使用日期过滤器时遇到如下问题

2018-07-21 22:49:24 +08:00
 getlost
当我在过滤器中使用小时(H)时,如:
<p>{{ entry.date_added|date:'Y-m-d H:i ' }}</p>
会报如下错误:
The format for date objects may not contain time-related format specifiers (found 'H').
去掉 H:i 后,错误消失
4542 次点击
所在节点    Python
10 条回复
matrix1010
2018-07-21 22:55:54 +08:00
因为是 Date, 只有年月日, 没有小时
chengxiao
2018-07-21 23:15:51 +08:00
看看 model 里定义的时间类型错了
Zzdex
2018-07-21 23:35:28 +08:00
看看你的 `date_added`是`DatetimeField` 还是 `Datefield`
inoki
2018-07-22 17:59:18 +08:00
见#3
models.DatetimeField
<=>
models.DateField
getlost
2018-07-22 22:34:50 +08:00
@matrix1010 @chengxiao 谢谢各位老哥
getlost
2018-07-22 22:37:54 +08:00
@Zzdex @inoki 两位老哥思路是对的,但是我将 models.DateField 改为 models.DateTimeField 出现了新的错误,下面是报错
Internal Server Error: /topics/1/
Traceback (most recent call last):
File "d:\t_django\ll_env\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "d:\t_django\ll_env\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "d:\t_django\ll_env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "d:\t_django\t_djangos\views.py", line 20, in topic
topic = Topic.objects.get(id=topic_id)
File "d:\t_django\ll_env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 397, in get
num = len(clone)
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 254, in __len__
self._fetch_all()
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 62, in __iter__
for row in compiler.results_iter(results):
File "d:\t_django\ll_env\lib\site-packages\django\db\models\sql\compiler.py", line 1011, in apply_converters
value = converter(value, expression, connection)
File "d:\t_django\ll_env\lib\site-packages\django\db\backends\sqlite3\operations.py", line 222, in convert_datetimefield_value
value = parse_datetime(value)
File "d:\t_django\ll_env\lib\site-packages\django\utils\dateparse.py", line 107, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
[22/Jul/2018 22:33:12] "GET /topics/1/ HTTP/1.1" 500 101852
Internal Server Error: /topics/1/
Traceback (most recent call last):
File "d:\t_django\ll_env\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "d:\t_django\ll_env\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "d:\t_django\ll_env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "d:\t_django\t_djangos\views.py", line 20, in topic
topic = Topic.objects.get(id=topic_id)
File "d:\t_django\ll_env\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 397, in get
num = len(clone)
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 254, in __len__
self._fetch_all()
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 1179, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "d:\t_django\ll_env\lib\site-packages\django\db\models\query.py", line 62, in __iter__
for row in compiler.results_iter(results):
File "d:\t_django\ll_env\lib\site-packages\django\db\models\sql\compiler.py", line 1011, in apply_converters
value = converter(value, expression, connection)
File "d:\t_django\ll_env\lib\site-packages\django\db\backends\sqlite3\operations.py", line 222, in convert_datetimefield_value
value = parse_datetime(value)
File "d:\t_django\ll_env\lib\site-packages\django\utils\dateparse.py", line 107, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object
Zzdex
2018-07-22 23:06:47 +08:00
@getlost #6 makemigrations&migrate 没....
getlost
2018-07-22 23:47:55 +08:00
@Zzdex @inoki 是我搞错了,没有迁移数据,但是还是有问题
----------------------------HTML---------------------------
{% extends "t_djangos/base.html" %}

{% block content %}

<p>Topics:{{ topic }}</p>

<p>Entries:</p>
<ul>
{% for entry in entries %}
<li>
<p>{{ entry.date_added|date:'M d, Y' H:i }}</p>
<p>{{ entry.text|linebreaks }}</p>
</li>
{% empty %}
<li>
There are no entriesfor this topic yet.
</li>
{% endfor %}
</ul>

{% endblock content %}
----------------------------HTML---------------------------
<p>{{ entry.date_added|date:'M d, Y' H:i }}</p> 这一行并不在网页上显示出来
<p>{{ entry.text|linebreaks }}</p> 这个却会
inoki
2018-07-23 04:56:03 +08:00
@getlost
先不加 filter 试一下。
:'M d, Y' H:i 这边引号不知道是不是你 po 出来时候的问题。
如果还是没有内容的话 db_shell 进去看看是不是迁移过后表里值没了
getlost
2018-07-23 21:37:22 +08:00
@inoki 原来是 views 里面把变量名写错了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/472986

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX