我不断获得预期的字符串或字节对象'错误,我不知道为什么。有人可以帮忙吗?谢谢!
查看
data = request.POST.get('data')
data_an = time.strptime(request.POST['data'], "%m/%d/%y")
newanal = Analysis(data=data_an)
TEMPLATE
<tr>
<td>Data (DD/MM/YY)</td>
<td><input type="text" name="data" /></td>
</tr>
MODEL
class Analysis(models.Model):
data = models.DateField(null=True)
我会从模板中插入并保存数据,但是当我点击“保存”时,我就有了预期的字符串或字节对象&#39;
data_an
time.struct_time(tm_year=2015, tm_mon=2, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=43, tm_isdst=-1)
newanal
data
'02/12/15'
request
('<WSGIRequest\n'
'path:/addanalysis/,\n'
'GET:<QueryDict: {}>,\n'
"POST:{'csrfmiddlewaretoken': '5O3kV1Z9iY6SJpIT47Dk1ITcJlWXmOt8',\n"
" 'data': '02/12/15',\n"
"COOKIES:{'csrftoken': '5O3kV1Z9iY6SJpIT47Dk1ITcJlWXmOt8',\n"
" 'sessionid': '296m76ca2ppjkllkmqmeopgjymktl7e1'},\n"
"META:{'AGSDESKTOPJAVA': 'C:\\\\Program Files "
"(x86)\\\\ArcGIS\\\\Desktop10.0\\\\',\n"
" 'ALLUSERSPROFILE': 'C:\\\\ProgramData',\n"
" 'APPDATA': 'C:\\\\Users\\\\Asus\\\\AppData\\\\Roaming',\n"
" 'CLASSPATH': "
"'C:\\\\xpressmp\\\\lib\\\\xprs.jar;C:\\\\xpressmp\\\\lib\\\\xprb.jar;C:\\\\xpressmp\\\\lib\\\\xprm.jar;',\n"
" 'COMMONPROGRAMFILES': 'C:\\\\Program Files\\\\Common Files',\n"
" 'COMMONPROGRAMFILES(X86)': 'C:\\\\Program Files (x86)\\\\Common Files',\n"
" 'COMMONPROGRAMW6432': 'C:\\\\Program Files\\\\Common Files',\n"
" 'COMPUTERNAME': 'ASUS-PC',\n"
" 'COMSPEC': 'C:\\\\WINDOWS\\\\system32\\\\cmd.exe',\n"
" 'CONFIGSETROOT': 'C:\\\\WINDOWS\\\\ConfigSetRoot',\n"
" 'CONTENT_LENGTH': '199',\n"
" 'CONTENT_TYPE': 'application/x-www-form-urlencoded',\n"
" 'CSRF_COOKIE': '5O3kV1Z9iY6SJpIT47Dk1ITcJlWXmOt8',\n"
" 'DJANGO_SETTINGS_MODULE': 'fileconverter.settings',\n"
" 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer',\n"
" 'FPS_BROWSER_USER_PROFILE_STRING': 'Default',\n"
" 'FP_NO_HOST_CHECK': 'NO',\n"
" 'GATEWAY_INTERFACE': 'CGI/1.1',\n"
" 'HOMEDRIVE': 'C:',\n"
" 'HOMEPATH': '\\\\Users\\\\Asus',\n"
" 'HTTP_ACCEPT': "
"'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',\n"
" 'HTTP_ACCEPT_ENCODING': 'gzip, deflate',\n"
" 'HTTP_ACCEPT_LANGUAGE': 'it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4',\n"
" 'HTTP_CACHE_CONTROL': 'max-age=0',\n"
" 'HTTP_CONNECTION': 'keep-alive',\n"
" 'HTTP_COOKIE': 'sessionid=296m76ca2ppjkllkmqmeopgjymktl7e1; '\n"
" 'csrftoken=5O3kV1Z9iY6SJpIT47Dk1ITcJlWXmOt8',\n"
" 'HTTP_HOST': '127.0.0.1:8000',\n"
" 'HTTP_ORIGIN': 'http://127.0.0.1:8000',\n"
" 'HTTP_REFERER': 'http://127.0.0.1:8000/addanalisi/',\n"
" 'HTTP_UPGRADE_INSECURE_REQUESTS': '1',\n"
" 'HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 "
"'\n"
" '(KHTML, like Gecko) Chrome/47.0.2526.106 "
"Safari/537.36',\n"
" 'INCLUDE': 'C:\\\\Program Files (x86)\\\\Microsoft Visual '\n"
" 'Studio\\\\VC98\\\\atl\\\\include;C:\\\\Program Files "
"(x86)\\\\Microsoft '\n"
" 'Visual Studio\\\\VC98\\\\mfc\\\\include;C:\\\\Program Files '\n"
" '(x86)\\\\Microsoft Visual Studio\\\\VC98\\\\include',\n"
" 'LIB': 'C:\\\\Program Files (x86)\\\\Microsoft Visual '\n"
" 'Studio\\\\VC98\\\\mfc\\\\lib;C:\\\\Program Files (x86)\\\\Microsoft "
"Visual '\n"
" 'Studio\\\\VC98\\\\lib',\n"
" 'LOCALAPPDATA': 'C:\\\\Users\\\\Asus\\\\AppData\\\\Local',\n"
" 'LOGONSERVER': '\\\\\\\\MicrosoftAccount',\n"
" 'NUMBER_OF_PROCESSORS': '8',\n"
" 'OS': 'Windows_NT',\n"
" 'PATH': 'C:\\\\Program Files (x86)\\\\Cuminas\\\\Document Express DjVu '\n"
' '
"'Plug-in\\\\;C:\\\\ProgramData\\\\Oracle\\\\Java\\\\javapath;C:\\\\Program "
"Files '\n"
" '(x86)\\\\NVIDIA '\n"
" 'Corporation\\\\PhysX\\\\Common;C:\\\\xpressmp\\\\bin;C:\\\\Program "
"Files '\n"
" '(x86)\\\\Intel\\\\iCLS Client\\\\;C:\\\\Program "
"Files\\\\Intel\\\\iCLS '\n"
" 'Client\\\\;C:\\\\Program Files\\\\Common Files\\\\Microsoft "
"Shared\\\\Windows '\n"
" 'Live;C:\\\\Program Files (x86)\\\\Common Files\\\\Microsoft '\n"
" 'Shared\\\\Windows '\n"
' '
"'Live;C:\\\\WINDOWS\\\\system32;C:\\\\WINDOWS;C:\\\\WINDOWS\\\\System32\\\\Wbem;C:\\\\WINDOWS\\\\System32\\\\WindowsPowerShell\\\\v1.0\\\\;C:\\\\Program "
"'\n"
" 'Files (x86)\\\\Windows Live\\\\Shared;C:\\\\Program Files '\n"
" '(x86)\\\\Intel\\\\OpenCL SDK\\\\2.0\\\\bin\\\\x86;C:\\\\Program "
"Fi... <trimmed 7923 bytes string>
mat
'1'
data_ca
time.struct_time(tm_year=2015, tm_mon=2, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=43, tm_isdst=-1)
答案 0 :(得分:1)
在创建与time.struct_time
对应的Analysis
对象时,您正在传递models.DateField
个对象。日期字段不知道如何将time.struct_time
转换为支持的日期字符串,因此错误。
改为使用datetime.date
对象。
from datetime import datetime
data_an = datetime.strptime(request.POST['data'], "%m/%d/%y")
newanal = Analysis(data=data_an)
newanal.save()
或者,您可以构造格式为YYYY-MM-DD
的有效日期字符串,并将其传递给构造函数:
data_an = time.strptime(request.POST['data'], "%m/%d/%y")
ISO_date = '{}-{:02}-{:02}'.format(data_an.tm_year, data_an.tm_mon, data_an.tm_mday)
newanal = Analysis(data=ISO_date)
但我认为前者更容易。