django - How to keep files in the formset after page reload? . Django
问题描述
Having spent few days trying to figure it out. Maybe it is a simply question for you, but, anyway…</p>
I have a view for the form within inline formset that receives and saves files + few text fields in a primary model form itself.
Problem is when form + formset are filled with text data for the main form and files for the formset, in case if main form is not valid, page gets reloaded and files from formset would get lost and I have to attach them again. I would like to have this situation fixed in a way that files after re rendering of the page would be present as well, but, unfortunatelli i can not find the solution.
My view is as following:
# only the portion that in charge for the above-mentioned logic
# files are getting lost in form2. With form 1 everything is OK
def viewname(request):
if request.method == 'POST':
form1 = MainForm(request.POST, request.FILES, prefix="form1")
if form1.is_valid():
prim = form1.save(commit=False)
prim.author = request.user
form2 = inline_formset(request.POST, request.FILES,
prefix="form2", instance=prim)
else:
form2 = inline_formset(request.POST, request.FILES, prefix="form2")
context = {"form1": form1, "form2": form2}
return render(request, "create.html", context)
…
…
…
reload of the page is caused by clean() method in forms. It validates 2 fields mutual data, so that I cant move this validation to validators in form fields. Need to get cleaned data to compare 2 fields anyway.
解决方案
可以通过应用程序“file_resubmit”来完成
推荐阅读
- oracle - Cloudformation AWS:将 RDS 连接到子网
- javascript - Webpack vue.js中如何将外部js集成到main.js文件中?
- mongodb - mongodb $lookup 3 级嵌套文档
- php - Google Translate API - html 文档翻译
- python - 玩家滑行和平台无法正确移动(Python/Pygame)
- c# - 我创建项目后,Android 应用程序不会立即编译
- python - wxPython - 更改网格对象的格式和外观
- php - 当我连接到 WIFI 时获取当前时间返回正常,如果我连接到 LTE,api 响应时间会出错
- scons - 使用 SCONS 构建多个目标
- go - 记录响应主体以用于日志记录