首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正试着用django上传图片,但它给了我/index 'myfile‘的文件。

我正试着用django上传图片,但它给了我/index 'myfile‘的文件。
EN

Stack Overflow用户
提问于 2021-06-28 08:22:31
回答 1查看 18关注 0票数 0

我试图插入图片到sqlite,但它不工作,它总是给我错误,这是我的代码

代码语言:javascript
复制
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')

这就是错误

代码语言:javascript
复制
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,但它不工作,它总是给我错误

EN

回答 1

Stack Overflow用户

发布于 2021-06-28 09:00:02

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68156270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档