0

I keep getting the 'expected string or bytes-like object ' error and i don't know why. Can anybody help? Thanks!

VIEW

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)

I would insert and save data from template, but when i click Save, i have 'expected string or bytes-like object'

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)
4
  • 4
    If you "keep getting an error", you should post it along with the full traceback. Commented Jan 14, 2016 at 13:18
  • Are you working in Python 2.7 oder 3.X? As far as I know they handle strings in different ways. Commented Jan 14, 2016 at 13:27
  • i'm working with python 3.5 Commented Jan 14, 2016 at 13:53
  • The additional information does not show the error. Anyway, have you tried using a datetime.date instead of a time.struct_time? Commented Jan 14, 2016 at 13:56

1 Answer 1

2

You are passing a time.struct_time object when creating the Analysis object which corresponds with a models.DateField. The date field doesn't know how to convert the time.struct_time to a supported date string, hence the error.

Use a datetime.date object instead.

from datetime import datetime

data_an = datetime.strptime(request.POST['data'], "%m/%d/%y")
newanal = Analysis(data=data_an)
newanal.save()

Alternatively you could construct a valid date string with format YYYY-MM-DD, and pass that to the constructor:

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)

but I think the former is easier.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.