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 · 4554 次点击
    这是一个创建于 2317 天前的主题,其中的信息可能已经有所发展或是发生改变。
    当我在过滤器中使用小时(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   ·   实用小工具   ·   2871 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 09:06 · PVG 17:06 · LAX 01:06 · JFK 04:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.