filefield

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

Django FileField default file

自古美人都是妖i 提交于 2021-02-16 05:12:20
问题 I have a model which contains FileField as below class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos') The question is how can I add a default file like "{{ MEDIA_ROOT}}/logos/anonymous.jpg" to this filefield ? 回答1: You can specify the default file to use for that field as follows: class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos', default=

Django FileField default file

最后都变了- 提交于 2021-02-16 05:00:10
问题 I have a model which contains FileField as below class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos') The question is how can I add a default file like "{{ MEDIA_ROOT}}/logos/anonymous.jpg" to this filefield ? 回答1: You can specify the default file to use for that field as follows: class Employer(models.Model): logo = models.FileField(storage=FileSystemStorage(location=settings.MEDIA_ROOT), upload_to='logos', default=

Edit uploaded file (djangos FileField) using pre_save signal

筅森魡賤 提交于 2021-02-08 07:56:37
问题 I want to edit an uploaded file on byte level (i.e. searching and removing a certain byte sequence) before saving it. I have a pre_save signal set up in the following way: class Snippet(models.Model): name = models.CharField(max_length=256, unique=True) audio_file = models.FileField(upload_to=generate_file_name, blank=True, null=True) @receiver(models.signals.pre_save, sender=Snippet) def prepare_save(sender, instance, **kwargs): if instance.audio_file: remove_headers(instance) Now I have had

Admin FileField current url incorrect

血红的双手。 提交于 2021-02-06 11:22:45
问题 In the Django admin, wherever I have a FileField, there is a "currently" box on the edit page, with a hyperlink to the current file. However, this link is appended to the current page url, and therefore results in a 404 as there is no such page as, for example: http://127.0.0.1:8000/admin/Tank/asset/17/media/datasheet/13/09/05/copyright.html/ For reference, the correct url of the file is: http://127.0.0.1:8000/media/datasheet/13/09/05/copyright.html Is there any way to fix this problem in the

How to get the files uploaded in InMemoryUploadedFile django

不想你离开。 提交于 2021-01-27 05:39:17
问题 I am developing an application in django 2.1 in which I must upload an undetermined number of audios through a modal and then pass the information to the view from which the modal is launched. However, these audios should not be stored in the database until the main view form is filled out. Then I thought about these solutions: First I thought about saving it as a session attribute but the contents of a FileField is not JSON serializable which did not work. Second I thought about the

Django: generate a CSV file and store it into FileField

风流意气都作罢 提交于 2020-06-16 02:24:31
问题 In my Django View file, I need to generate a CSV file from a list of tuples and store the CSV file into the FileField of my model. class Bill(models.Model): billId = models.IntegerField() bill = models.FileField(upload_to='bills') I searched on this site, and found some posts such as Django - how to create a file and save it to a model's FileField?, but the solutions can help me. I need the CSV file to be stored in 'media/bills/' fold, and I hope the CSV file can be deleted together with the

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

How to localize the label of a file_field in Rails?

a 夏天 提交于 2020-01-17 08:17:27
问题 I want to localize a form in my app so that all the labels are in Finnish. This is easy with all other form components, but how do I do this with a file field? It always seems to give me a label "choose file" in the button and "no file chosen" immediately after the button. 回答1: This dumb file field depends only on your system locale. You cannot change way it looks or behaves, which annoys many of us, not to mention the fact that it can look completely different across different platforms. To