开发者

Django file upload...why is this working?

This is the first time I've tried to upload a file with Django. I did something and it worked, th开发者_Python百科ough I realized later that it's not the correct way to do it. When I called save on the object, did it call a built-in handler for the FileField? I realize that I should create my own handler but I was just curious why this worked.

def upload_test(request):
user=User.objects.get(pk=user.id)
photoform=PhotoForm()
if request.method=='POST':
    photoform=Post_PhotoForm(request.POST,request.FILES)
    if photoform.is_valid():
        photo=photoform.save(commit=False)
        photo.user=user
        photo.save()
        return HttpResponse('success')
    else:
        return HttpResponse('%s' %photoform.errors)
return render_to_response("site/upload_test.html", {'photoform':photoform}, context_instance=RequestContext(request))   

This is saving the object and uploading the file to the directory specified in the FileField.

If I create a handler which writes the file in chunks, how can I also save the photoform instance? Will it create duplicates?

Thanks for the insight.


I imagine PhotoForm is a ModelForm? Manual handling of uploaded files as described in the docs is only required for standard Forms. The chunk handling is performed in the background by the models.FileField and its storage object, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜