我试图插入图片到sqlite,但它不工作,它总是给我错误,这是我的代码
def index(request):
dbname = 'user1'
cursor = connections[dbname].cursor()
if request.POST.get('done'):
cursor.execute('insert into images values(%s)', [request.FILES['myfile']])
return render(request, 'index.html')这就是错误
MultiValueDictKeyError at /index
'myfile'
Request Method: POST
Request URL: http://127.0.0.1:8000/index
Django Version: 3.2
Exception Type: MultiValueDictKeyError
Exception Value:
'myfile'
Exception Location: D:\omar\pythonProject\venv\lib\site-packages\django\utils\datastructures.py, line 78, in __getitem__
Python Executable: D:\omar\pythonProject\venv\Scripts\python.exe
Python Version: 3.9.2
Python Path:
['D:\\omar\\pythonProject',
'C:\\Users\\taha_lab\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\taha_lab\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\taha_lab\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\taha_lab\\AppData\\Local\\Programs\\Python\\Python39',
'D:\\omar\\pythonProject\\venv',
'D:\\omar\\pythonProject\\venv\\lib\\site-packages']
Server time: Mon, 28 Jun 2021 00:18:52 +0000
Traceback Switch to copy-and-paste view
D:\omar\pythonProject\venv\lib\site-packages\django\utils\datastructures.py, line 76, in __getitem__
list_ = super().__getitem__(key) …
▶ Local vars
During handling of the above exception ('myfile'), another exception occurred:
D:\omar\pythonProject\venv\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
response = get_response(request) …
▶ Local vars
D:\omar\pythonProject\venv\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
D:\omar\pythonProject\Dhb\views.py, line 5951, in index
cursor.execute('insert into images values(%s)', [request.FILES['myfile']]) …
▶ Local vars
D:\omar\pythonProject\venv\lib\site-packages\django\utils\datastructures.py, line 78, in __getitem__
raise MultiValueDictKeyError(key) 我试图插入图片到sqlite,但它不工作,它总是给我错误
发布于 2021-06-28 09:00:02
Django有ORM是有原因的,使用imagefield或filefield https://docs.djangoproject.com/en/3.2/topics/files/存储也可以使用。
https://stackoverflow.com/questions/68156270
复制相似问题