使用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', {})
这时当我加密数据并尝试保存到数据库中时,上述错误即将到来。请帮助我解决此错误。
答案 0 :(得分:1)
可能会this answer对您有所帮助。
或者您可以使用base64
对字符串中的编码密码进行编码:
import base64
# your code before if statement is here
b64enpass = base64.b64encode(enpass)
if sec_pass == con_password:
passw = User(
uname=name,
password=b64enpass,
raw_password=password,
)
passw.save()
但当然在这种情况下,您必须在从db。
接收User
个对象后解码密码