Django“预期的字符串或缓冲区”是什么意思?错误提示?
我已经被这个错误困扰了一段时间了,我只是不明白它意味着什么。当我尝试将对象保存到 mysql 数据库时会发生这种情况。有什么想法吗?
感谢您的帮助!
I've been stuck with this error for quite a while now and I just can't figure out what it means. It occurs when I try to save an object to my mysql database. Any ideas?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
刚刚遇到了同样的问题并解决了。我实例化了一个这样的表单:
该表单稍后保存,django 尝试在模型中设置“日期”。但显然,datetime.now 指的是函数而不是日期。我想做的是 datetime.now()
也许这可以帮助任何人将来遇到这个问题。
Just ran into the same problem and resolved it. I instantiated a form like this:
That form was saved later on and django tried to set 'date' in the model. But datetime.now refers to a function rather than a date, obviously. What I wanted to do was datetime.now()
Maybe this helps anybody running into this in future.
这可能意味着 Python 正在尝试执行需要某种数据类型(bool、string、int 等)的代码,但提供了其他不正确的数据类型。
This probably means that Python is trying to execute code which is expecting a certain datatype (bool, string, int, etc.), but an other, incorrect, datatype is provided.
就我而言,当我使用“时间”库将日期字符串转换为日期时间对象时,就会出现这种情况。我只是使用“datetime.strptime”而不是“time.strptime”,问题就消失了。
In my case it was appears when I use "time" library to convert date string to datetime object. I just use "datetime.strptime" instead of "time.strptime"and problem vanished.
django 中的日期时间验证器位于文件:
或当前 python 解释器的站点包中。
看看那里的正则表达式。就我而言,解决这个问题的方法是:
两者都有效。
The datetime validator in django is in the file:
or in the site-packages of your current python interpreter.
Have a look there to see the regular expressions. In my case, the way to solve it was:
both working.
“expiration”是这个字段
,有错误的代码是这个:
通过这样设置(而不是 timedelta,日期)解决了这个问题
'expiration' is this field
And code with error was this one:
And it was solved by setting it like this (instead of timedelta, a date)
您可以转换您的变量。
Ou can convert sting your variables.