【发布时间】:2017-06-26 14:27:49
【问题描述】:
我在使用 Django 和 Python 将数据保存到数据库时出错。错误如下。
错误:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/lampp/htdocs/d30/carClinic_vulnerable/bookingservice/views.py", line 153, in signsave
passw.save()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 806, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 836, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 922, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 961, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1063, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 84, in execute
sql = self.db.ops.last_executed_query(self.cursor, sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/operations.py", line 135, in last_executed_query
params = self._quote_params_for_last_executed_query(params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/operations.py", line 124, in _quote_params_for_last_executed_query
return cursor.execute(sql, params).fetchone()
ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
我在下面解释我的模型文件。
class User(models.Model):
"""docstring for User"""
uname = models.CharField(max_length=200)
password = models.CharField(max_length=200)
date = models.DateTimeField(default=datetime.now, blank=True)
raw_password = models.CharField(max_length=200,blank=True, null=True)
这是要保存到数据库中的数据。
if request.method == 'POST':
name = request.POST.get('uname')
password = request.POST.get('pass')
sec_pass = password
con_password = request.POST.get('conpass')
length = 16 - (len(password) % 16)
password += chr(length)*length
obj = AES.new('this is a carkey', AES.MODE_CBC, 'This is an IV456')
enpass = obj.encrypt(password)
if sec_pass == con_password:
passw = User(
uname=name,
password=enpass,
raw_password=password,
)
passw.save()
return render(request, 'bookingservice/login.html', {})
当我加密数据并尝试保存到数据库中时,出现上述错误。请帮助我解决此错误。
【问题讨论】:
-
你为什么要这样做? Django 已经有一个非常好的和安全的身份验证系统。你为什么要玩加密货币?
-
我有一个这样的要求。你能解决这个错误吗?
-
我猜
enpass是字节,实际上不是字符串。您可能需要对其进行解码? -
@Grimmy:你能写出你的解决方案吗?
-
base64 绝对是这里的答案,正如@EduardStepanov 在答案中提到的那样。
encrypt返回二进制数据,不能解码成字符串。