我有这段代码:
vehicle = get_object_or_404(Vehicle, stock_number=stock_number)
if request.method == 'POST':
vehicle_form = VehicleForm(request.POST, instance=vehicle)
photos = PhotosFormSet(request.POST, request.FILES, instance=vehicle)
if vehicle_form.is_valid() and photos.is_valid():
vehicle = vehicle_form.save()
photos.save()
request.user.message_set.create(message='The vehicle "%s" was edited
successfully.' % vehicle.__str__())
return HttpResponseRedirect("/vehicles/details/%s/" % stock_number)
else:
vehicle_form = VehicleForm(instance=vehicle)
photos = PhotosFormSet(instance=vehicle)
return render_to_response('vehicles/vehicles-add-edit.html',
{'vehicle_form':vehicle_form, 'photos': photos,},
context_instance=RequestContext(request))
除ImageField
中的PhotosFormSet
外,表单已正确初始化。
答案 0 :(得分:3)
当你说它没有被初始化时,你是什么意思?你的意思是填充原始数据吗?
HTML文件输入字段永远不会填充现有数据 - 这是一个浏览器限制,与Django无关。这是一种安全措施,可以阻止恶意网站诱使您从计算机上传任意内容。