【问题标题】:i am trying to upload image in django but it gives me MultiValueDictKeyError at /index 'myfile'我正在尝试在 django 中上传图像,但它在 /index 'myfile' 处给了我 MultiValueDictKeyError
【发布时间】:2021-06-28 00:22:31
【问题描述】:

我试图将图像插入到 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,但它没有用它总是给我错误

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    Django 有 ORM 是有原因的,使用 imagefield 或 filefield https://docs.djangoproject.com/en/3.2/topics/files/ 也可以使用存储。

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 2022-07-21
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      相关资源
      最近更新 更多