Computer for Everybody

Upload Simple

Create Form

<form enctype="multipart/form-data" method="post">
    {% csrf_token %}
    <div>
        <label>Image</label>
        <input class="form-control" type="file" name="f" id="f" accept="image/*">
    </div>
    <div>
        <button>Upload</button>
    </div>
</form>

Process in views

def upload(req):
    if req.FILES:
        f = req.FILES['f']
        with open(f'./static/images/{f.name}', 'wb') as des:
            for chunk in f.chunks():
                des.write(chunk)
        return render(req, 'upload.html', {'img': f.name})
    render render(req, 'upload.html')

Config in settings

from os.path import join
STATICFILES_DIRS = (join(BASE_DIR, STATIC_URL), STATIC_URL)