Imagefield in Inline formset not populated

旧街凉风 提交于 2019-12-12 04:25:40

问题


I have this piece of code:

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))

The forms are initialized correctly except the ImageField in PhotosFormSet.


回答1:


What do you mean when you say it's not initialized? Do you mean populated with the original data?

HTML file input fields are never populated with existing data - this is a browser restriction, nothing to do with Django. It's a security measure, to stop malicious sites tricking you into uploading arbitrary content from your computer.



来源:https://stackoverflow.com/questions/2206617/imagefield-in-inline-formset-not-populated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!