一、ImportError
刚创建一个Django项目时,马上新建一个app时出现的一个错误。
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH en
解决方法:
检查有没有Django这个包(有时候需要自己手动导入)
二、ImproperlyConfigured
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
详细错误就不站出来,太长了,部分如代码。
原因:Django默认使用的Sqlite,而笔者使用的时 MySQL ,且仅仅在 settings.py 的 DATABASES 做了修改后。
解决方法:
在项目同名下的 _init_.py 文件中加入以下代码:
import pymysql
pymysql.install_as_MySQLdb()
三、ValueError
ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.
原因:tenant查询的结果是一个结果集(QuerySet),在Order.objects.filter(tenant=tenant, status_id=2)的筛选条件必须是一个对象而不是一个结果集。
解决方法:
tenant = Tenant.objects.filter(name=name)改为tenant = Tenant.objects.filter(name=name).first()
tenant = Tenant.objects.filter(name=name)
# 有该用户
if tenant.exists():
book_orders = Order.objects.filter(tenant=tenant, status_id=2)
print(book_orders)
File "E:\Code\python workplace\Hotel\manageRoom\views.py", line 156, in check_in_search
print(book_orders)
(中间省略)
File "E:\Code\python workplace\Hotel\venv\lib\site-packages\django\db\models\lookups.py", line 257, in process_rhs
'The QuerySet value for an exact lookup must be limited to '
ValueError: The QuerySet value for an exact lookup must be limited to one result using slicing.