在练习使用 Django,使用日期过滤器时遇到如下问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
getlost
V2EX    Python

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

  •  
  •   getlost 2018-07-21 22:49:24 +08:00 5054 次点击
    这是一个创建于 2688 天前的主题,其中的信息可能已经有所发展或是发生改变。
    当我在过滤器中使用小时(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 后,错误消失
    10 条回复    2018-07-23 21:37:22 +08:00
    matrix1010
        1
    matrix1010  
       2018-07-21 22:55:54 +08:00 via Android
    因为是 Date, 只有年月日, 没有小时
    chengxiao
        2
    chengxiao  
       2018-07-21 23:15:51 +08:00
    看看 model 里定义的时间类型错了
    Zzdex
        3
    Zzdex  
       2018-07-21 23:35:28 +08:00
    看看你的 `date_added`是`DatetimeField` 还是 `Datefield`
    inoki
        4
    inoki  
       2018-07-22 17:59:18 +08:00 via Android
    见#3
    models.DatetimeField
    <=>
    models.DateField
    getlost
        5
    getlost  
    OP
       2018-07-22 22:34:50 +08:00
    @matrix1010 @chengxiao 谢谢各位老哥
    getlost
        6
    getlost  
    OP
       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
        7
    Zzdex  
       2018-07-22 23:06:47 +08:00 via iPhone
    @getlost #6 makemigrations&migrate 没....
    getlost
        8
    getlost  
    OP
       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
        9
    inoki  
       2018-07-23 04:56:03 +08:00 via Android
    @getlost
    先不加 filter 试一下。
    :'M d, Y' H:i 这边引号不知道是不是你 po 出来时候的问题。
    如果还是没有内容的话 db_shell 进去看看是不是迁移过后表里值没了
    getlost
        10
    getlost  
    OP
       2018-07-23 21:37:22 +08:00
    @inoki 原来是 views 里面把变量名写错了
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     874 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 22:41 PVG 06:41 LAX 14:41 JFK 17:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86