gpt4 book ai didi

python - 获取 'str' 到 Django forms.FileField()

转载 作者:行者123 更新时间:2023-12-05 08:04:11 27 4
gpt4 key购买 nike

我在没有表格的情况下上传。如果我通过 <input type...> 上传,我的用户有一个 FileField我没有问题。但这对我的目的来说不是那么方便。

View .py

    def uploadaction2(request):
f = forms.FileField() # this is my idea
data = json.loads(request.body)
# f.clean("data['data']") # error: file_name = data.name AttributeError: 'str' object has no attribute 'name'
logger.error(type(data['data'])) #gives <class 'str'> and without the type the string that i want to save
with open('newfile', 'w') as file:
file.write(data['data'])
request.user.file = file
request.user.save() # i get no error until this part

错误

Internal Server Error: ....  in pre_save
if file and not file._committed:
AttributeError: '_io.TextIOWrapper' object has no attribute '_committed'

我想我必须将 json 字符串转换为 forms.FileField(),但我不知道该怎么做。

最佳答案

    def uploadaction2(request):
file = request.FILES.get('file') # set name to input
request.user.file = file
request.user.save()

关于python - 获取 'str' 到 Django forms.FileField(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69848596/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com