I am creating and saving a PDF as such in my views:
views.py
#login_required(login_url="/login")
def PackingListView(request):
if request.method == "POST":
form = PackingListForm(request.POST)
if form.is_valid():
if 'preview' in request.POST:
...
elif 'save' in request.POST:
pdf_contents = form
file = ContentFile(pdf_contents)
item = PackingListDocuments.objects.get(pk=1)
item.PackingListDocument.save('test.pdf', file) #saving as FileField in model
form.save()
messages.success(request, "Success: Packing List Has Been Created!")
return redirect('HomeView')
I see that the test.pdf is saved. I can see it in my file explorer as well as in the admin, but every time that I attempt to open it, the file seems to be corrupted. What do I need to add or subtract in my code to get this working?
Thanks!
UPDATE:
I've changed the line: file = ContentFile(pdf_contents) to file = File(pdf_contents)
But now I am receiving an attribute error that 'PackingListForm' object has no attribute 'read'
I believe the error must be to do with this line
file = ContentFile(pdf_contents)
Note that, from the docs
The ContentFile class inherits from File, but unlike File it operates on string content (bytes also supported), rather than an actual file. For example:
So my guess is that you are not passing in a string/byte type as argument to the ContenetFile object.
Try finding the type of it. You can also convert it to string type by doing String(pdf_contents).
Related
In my Django "sources" project I have the "bulletins" app.
I have a view "model_form_upload" inside of which I get the uploaded file name ("csvFilename") when I upload it.
views.py
from bulletins.forms import ErnageForm
from bulletins.models import ErnageModel
from bulletins.pretreatment import pretreatment
def model_form_upload(request):
if request.method == 'POST':
form = ErnageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
for filename, file in request.FILES.items():
csvFilename = file.name
resultPretreatment = pretreatment()
print(resultPretreatment)
return redirect('proceed_FT')
else:
return redirect('upload_fail')
else:
form = ErnageForm()
return render(request, 'bulletins/upload_csv.html', {'form': form})
Beside that, I have a python script "pretreatment.py" that effectuates a bunch of pandas-dataframe transformations on my csv file.
pretreatment.py
def pretreatment(csvFileToTreat="..."
#... all the transformations with pandas dataframes...
df.to_csv(txtExportFilePath, index=False, header=None, sep='\t')
models.py
class ErnageModel(models.Model):
csv = models.FileField(upload_to="METEO ERNAGE/{0}/{1}".format(yearToTreat,monthToTreat))
My csvFilename will always be a string of this type : "Ernageyyyymm.csv".
My question here is : How can I collect my csvFilename variable out of the views.model_form_upload function to use its date information it in the pretreatment python script variables "csvFileToTreat" and "txtExportFilePath".
Those two ones are of the type "path/Ernage{0}{1}.format(yearToTreat,monthToTreat).
As I have to use it each month I would like to have my files uploaded in media/METEO ERNAGE/{0}/{1}".format(yearNumber,monthNumber) as you can see in the FileField upload_to and then everything happening in this directory. So the pretreatment charges the uploaded file and then export in it also.
I hope all this is as clear as possible, thank you for your help.
store csvFilename name in a session like this:
request.session['file']=csvFilename
and you can use this session where ever you want like this.
csvFilename =request.session['file']
This is an admin form handler, for example I upload a test.txt file in the django admin panel:
def save_model(self, request, obj, form, change):
if 'file' in form.changed_data:
print("file has changed: ")
print(obj.file)
else:
print("file has not changed")
super(FileAdmin, self).save_model(request, obj, form, change)
here I get the original file name from the upload form, but by fact the file is saved with another name if there is already a file with this name, but in the above code i get only the original name in all cases, how to can I get the changed/updated file name that was saved?..
The "_somehash" part is added by your project's filestorage when he sees there's already a file by the same name in the destination directory. This happens when the model instance is saved, so if all you need is to read the "final" name, you can get it from your (saved) model field's .name attribute (I assume you use a FileField of course).
I generate a file in python, and want to "upload" that file to the django database. This way it is automatically put inside the media folder, and organized neatly with all other files of my application.
Now here is what I tried: (type hinting used, since it's python 3.6)
# forms.py
class UploadForm(forms.ModelForm):
class Meta:
model = UploadedFile
fields = ('document',)
# models.py
class UploadedFile(models.Model):
document = models.FileField(upload_to=get_upload_path)
# mimetype is generated by filename on save
mimetype = models.CharField(max_length=255)
# ... additional fields like temporary
def get_upload_path(instance: UploadedFile, filename):
if instance.temporary:
return "uploaded_files/temp/" + filename
return "uploaded_files/" + filename
# views.py, file_out has been generated
with open(file_out, 'rb') as local_file:
from django.core.files import File
form = UploadForm(dict(), {'document': File(local_file)})
print(form.errors)
if form.is_valid():
file = form.save(commit=False)
# ... set additional fields
file.save()
form.save_m2m()
return file
Now this is not the only thing I've tried. First I've gone with setting the FileField directly, but that resulted in the save() to fail, while the mimetype field is set. Because the original file sits outside the media folder, and thus a suspicious file action is triggered.
Also, the form gives some feedback about the "upload", through the form.errors.
Depending on my approach, either the save() fails as mentioned above -- meaning the "uploading" does not actually copy the file in the media folder -- or the form returns the error that no file was transmitted, and tells to check the form protocol.
Now my theory is, that I would have to go and initialize my own instance of InMemoryUploadedFile, but I could not figure out how to do that myself, and no documentation was available on the internet.
It feels like I'm taking the wrong approach from the get go. How would one do this properly?
Do you have get_upload_path defined? If not, that would explain the errors you're getting.
From what I can see you're on the right track. If you don't need a dynamic path for your uploads, if you just want them in media/uploads, you can pass in a string value for upload_to (from the Django docs):
# file will be uploaded to MEDIA_ROOT/uploads
document = models.FileField(upload_to='uploads/')
First of all, thanks to Franey for pointing me at storage documentation which lead me to contentfile documentation.
The ContentFile actually solves the problem, because it basically is the self-instantiated version of InMemoryUploadedFile that I was looking for. It's a django File that is not stored on disk.
Here's the full solution:
# views.py, file_out has been generated
with open(file_out, 'rb') as local_file:
from django.core.files.base import ContentFile
# we need to provide a name. Otherwise the Storage.save
# method reveives a None-parameter and breaks.
form = UploadForm(dict(), {'document': ContentFile(local_file.read(), name=name)})
if form.is_valid():
file = form.save(commit=False)
# ... set additional fields
file.save()
form.save_m2m()
return file
I've been struggling with file validation for a while now but I think I'm on the right track. I'm trying to validate an audio file 'sound' when I post a form. Whenever I try and submit the form I get an 'InMemoryUploadedFile' object has no attribute 'content'. The problem seems to happen at form.is_valid(). So far I have in forms:
class PostForm(forms.ModelForm):
def clean_sound(self):
file = self.cleaned_data.get('sound',False)
if file:
if not file.content-type in ["audio/mpeg","audio/mp3", "audio/wav"]:
raise ValidationError("Content-Type is not mpeg")
if not os.path.splitext(file.name)[1] in [".mp3",".wav"]:
raise ValidationError("Doesn't have proper extension")
return file
else:
raise ValidationError("Couldn't read uploaded file")
class Meta:
model = Places
fields = [
'usersave',
'title',
'longitude',
'latitude',
'sound',
]
in the view:
#login_required(login_url='/accounts/login/')
def post_create(request):
form= PostForm(request.POST or None, request.FILES or None)
if form.is_valid():
# handle_uploaded_file(request.FILES['sound'])
instance = form.save(commit=False)
instance.save()
messages.success(request, 'Successfully Created')
return HttpResponseRedirect('/')
context= {
'form': form,
}
return render(request, 'location/post_form.html',context,)
I'm not sure what the 'InMemoryUploadedFile' error is, and would much appreciate any advice that could point me in the right direction!
wanted to comment but don't have enough reputation yet.
If you take a look at the source code it seems you have to call open() first in order to access its content. The class instance itself file = self.cleaned_data.get('sound', False) doesn't have a content attribute as stated in the error. May you could try opening the file first?
try this
file = self.cleaned_data.get('sound')
and
are you trying to upload the same audio file and getting the error? or you tried with other audio files also?
I'm developing an application in python with django. User can upload a SQL file. I use fileField to get the file. But, it's not stored any where. I try to take it from request to process the file. While I'm trying to open the file, it gives an error. Request object which I try to take the uploaded file is also django designed object.
from mssql2postgresql.form import LoadForm
form = LoadForm(request.POST, request.FILES)
if form.is_valid():
docFile = request.FILES['docFile']
archivo = UploadedFile.name
print 'este es el file', docFile
The given error is :
coercing to Unicode: need string or buffer, InMemoryUploadedFile found
Files stored in request.FILES are streams, so they can't be just casted to strings. To print file content, you must read it first:
docFile = request.FILES['docFile'].read()