开发者

Django: Save an uploaded file to a FileField

I feel a little stupid for having to ask this… But I can't seem find it documented anywhere.

If 开发者_C百科I've got a Model with FileField, how can I stuff an uploaded FILE into that FileField?

For example, I'd like to do something like this:

class MyModel(Model):
    file = FileField(...)

def handle_post(request, ...):
    mymodel = MyModel.objects.get(...)
    if request.FILES.get("newfile"):
        mymodel.file = request.FILES["newfile"]

But that doesn't appear to work.


Well, my suspicions were confirmed: I am an idiot :)

The method I outline in my question is, in fact, correct — it wasn't working because I'd forgotten to include enctype="multipart/form-data" on the form.

Anyway, I'll leave this question here, just incase other people have the same problem.


I also had issues with file not really posted to server when name attribute was not specified in input tag

<input type="file" name="somename">
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜