[Django]-How to save edited content in a file correctly using FileField?

3👍

You just don’t assing a string to a FileField

file.source = content

you have to write the content to the file like this:

if request.method == "POST":
        from django.core.files import File
        f = open(file.source.path, 'w')
        content = request.POST['content']
        f.write(content)
        f = File(f)
        file.source = f
        file.save()

Hope this helps!

Leave a comment