X
X
X-User2016-10-10 17:05:07
Django
X-User, 2016-10-10 17:05:07

Can you please explain what this function does?

Greetings. Can you please explain what this piece of code does?
Interested in the moment with with open .... and the cycle itself

def upload_picture(request):
    try:
        f = request.FILES['picture']
        filename = django_settings.MEDIA_ROOT + '/profile_pictures/' + request.user.username + '_tmp.jpg'
        with open(filename, 'wb+') as destination:
            for chunk in f.chunks():
                destination.write(chunk)
            pass
            .....

Thanks to

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Gasoid, 2016-10-10
@X-User

when uploading, the file usually comes not in its entirety, but in pieces,
so the file is collected,
this code collects the file from pieces ,
but it is better to use it through the form for the model

form = ImageForm(request.POST, request.FILES)
if form.is_valid():
    form.save()

and clearer and easier

S
sts, 2016-10-10
@stunoff

https://docs.djangoproject.com/en/1.10/topics/http...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question