django authentication multi upload files
Xristos Xristoou
saxri89 at gmail.com
Thu Mar 30 11:06:48 EDT 2017
I have create I simple Django auth project and I need to add the user to can upload some images. multi upload images from internet
views.py
from django.shortcuts import render
from django.http import HttpResponse
def Form(request):
return render(request, "index/form.html", {})
def Upload(request):
for count, x in enumerate(request.FILES.getlist("files")):
def process(f):
with open('/Users/Michel/django_1.8/projects/upload/media/file_' + str(count), 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
process(x)
return HttpResponse("File(s) uploaded!")
but how to define that to multi upload images in specific unique folder for any user. first I use login_required and in destination I use user_directory_path. But how to define the code in the views.py to work with authentication per user. for example for user_1 upload images in your folder for user_1 in folder for user_2.
medels.py
def user_directory_path(instance, filename):
return 'user_{0}/{1}'.format(instance.user.id, filename)
class MyModel(models.Model):
user = models.ForeignKey(User, unique=True)
upload = models.ImageField(upload_to=user_directory_path)
More information about the Python-list
mailing list