django-file-upload

Easiest way to display uploaded (image)file?

匆匆过客 提交于 2021-02-19 06:27:30
问题 I'm trying to my first attempt at django file-uploading, and all i need at the moment is a way to display an uploaded image (png/jpg) to the user that uploaded. No need to save it anywhere. My views.py: (i'm using django forms, btw) if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): upFile = request.FILES['upFile'] f={"upFile":upFile} return render_to_response('upload2.html', f) And i can, as expected, display the name and size of my file in

Where/how to replace default upload handler in Django CBV?

谁都会走 提交于 2021-02-10 05:53:07
问题 I am trying to specify a specific method of handling file uploads for a class based view. Per the docs this can be achieved by something like: from django.core.files.uploadhandler import TemporaryFileUploadHandler request.upload_handlers = [TemporaryFileUploadHandler(request=request)] If i specify this in post method of a FormView like so: def post(self, request, *args, **kwargs): request.upload_handlers = [TemporaryFileUploadHandler(request=request)] return super().post(self, request, *args,

How to upload multiple files from the Django admin?

心已入冬 提交于 2020-12-04 06:40:32
问题 I want to upload multiple files in the Django admin, without having to place multiple FileField fields. The user can be able to manage the files in a simple way; delete or change each uploaded file but upload several at once. the solution I see viable is to use several filefield but the problem is, i dont know how many files the user will upload def case_upload_location(instance, filename): case_name = instance.name.lower().replace(" ", "-") file_name = filename.lower().replace(" ", "-")

upload multiple images without forms in django

本秂侑毒 提交于 2020-07-10 10:28:15
问题 This is my models.py class Upload(models.Model): images = models.ImageField(upload_to='image/') serializers.py class UploadSerializer(serializers.ModelSerializer): class Meta: model = Upload fields = '__all__' views.py class UploadViewSet(viewsets.ModelViewSet): queryset = Upload.objects.all() permission_classes = [ permissions.AllowAny ] serializer_class = UploadSerializer I can successfully upload single image, however django model is not recieving multiple images at once. How to upload

Django dynamic FileField upload_to

烂漫一生 提交于 2020-03-25 18:23:14
问题 I'm trying to make dynamic upload path to FileField model. So when user uploads a file, Django stores it to my computer /media/(username)/(path_to_a_file)/(filename). E.g. /media/Michael/Homeworks/Math/Week_1/questions.pdf or /media/Ernie/Fishing/Atlantic_ocean/Good_fishing_spots.txt VIEWS @login_required def add_file(request, **kwargs): if request.method == 'POST': form = AddFile(request.POST, request.FILES) if form.is_valid(): post = form.save(commit=False) post.author = request.user post

When editing an object in django the ImageField is not populated

偶尔善良 提交于 2020-02-27 12:04:09
问题 I'm trying to edit an existing object through a form. Every thing is working fine except for the ImageField not being populated with the current value. Here's the model: class Post(models.Model): author = models.ForeignKey(User, editable=False) slug = models.SlugField(max_length = 110, editable=False) title = models.CharField(verbose_name='Blog Title', max_length=40, blank=False) body = models.TextField(verbose_name='Body') thumbnail = models.ImageField(upload_to = 'posts/%Y/%m') Here's the

When editing an object in django the ImageField is not populated

那年仲夏 提交于 2020-02-27 12:03:22
问题 I'm trying to edit an existing object through a form. Every thing is working fine except for the ImageField not being populated with the current value. Here's the model: class Post(models.Model): author = models.ForeignKey(User, editable=False) slug = models.SlugField(max_length = 110, editable=False) title = models.CharField(verbose_name='Blog Title', max_length=40, blank=False) body = models.TextField(verbose_name='Body') thumbnail = models.ImageField(upload_to = 'posts/%Y/%m') Here's the

Passing parameter to upload_to function in Django

岁酱吖の 提交于 2020-01-04 06:13:14
问题 I try to upload multiple images of an instance to different sub folders. But I need to rename each uploaded file as well so I implement a function for upload_to field as below. class MyModel(models.Model): code = models.CharField() logo = models.FileField(upload_to=get_path) cover = models.FileField(upload_to=get_path) def get_path(instance, filename): ext = filename.split('.')[-1] new_name = "%s.%s" % (slughifi(filename), ext) return new_name However, I'm not sure how I can divide images to

Passing parameter to upload_to function in Django

流过昼夜 提交于 2020-01-04 06:08:10
问题 I try to upload multiple images of an instance to different sub folders. But I need to rename each uploaded file as well so I implement a function for upload_to field as below. class MyModel(models.Model): code = models.CharField() logo = models.FileField(upload_to=get_path) cover = models.FileField(upload_to=get_path) def get_path(instance, filename): ext = filename.split('.')[-1] new_name = "%s.%s" % (slughifi(filename), ext) return new_name However, I'm not sure how I can divide images to

Django: file field validation in model using python-magic

好久不见. 提交于 2020-01-04 02:15:30
问题 I have a model containing file field. I want to restrict it to pdf files. I have written clean method in model because I want to check for admin and shell level model creation also. But it is not working in model clean method. However form clean method is working. class mymodel(models.Model): myfile = models.FileField() def clean(): mime = magic.from_buffer(self.myfile.read(), mime=True) print mime if not mime == 'application/pdf': raise ValidationError('File must be a PDF document') class