I have a django app where user can upload a pdf and get it converted to .png. The upload part is working fine but when I try to convert the pdf file, it says file not found. I noticed something weird that the filename contains _ but when I print the name of the file uploaded, it converted _ to whitespace ( "file_1.pdf" to "file 1.pdf") and hence the reason for the error file not found! Why is django doing this? I haven't written any code to do so!!
models.py
from django.db import models
from django.contrib.auth.models import User
from django.core.validators import FileExtensionValidator
class UploadPdf(models.Model):
resumes = models.FileField(blank=True, null=True, validators=[FileExtensionValidator(allowed_extensions=['pdf'])])
forms.py
from django import forms
from app1.models import UploadPdf
from django.forms import ClearableFileInput
class ResumeUpload(forms.ModelForm):
class Meta:
model = UploadPdf
fields = ['resumes']
widgets = {
'resumes': ClearableFileInput(attrs={'multiple': True}),
}
views.py
from django.shortcuts import render, redirect
from app1.forms import ResumeUpload
from app1.models import UploadPdf
from app1.convert import convert_file
import os
def home(request):
if request.method == 'POST':
form = ResumeUpload(request.POST, request.FILES)
files = request.FILES.getlist('resumes')
if form.is_valid():
f = form.save()
f.user = request.user
f.save()
f_list = []
file_name = form.cleaned_data['resumes'].name
if files:
for i in files:
file_instance = UploadPdf(resumes=i)
f_list.append(file_instance.resumes.path)
file_instance.save()
for j in f_list:
convert_file(j)
else:
form = ResumeUpload()
return render(request, 'app1/home.html' , {'form': form})
convert.py
import fitz, os
def convert_file(f_path):
doc = fitz.open(f_path)
name = os.path.splitext(os.path.basename(f_path))[0]
for page in doc:
pix = page.get_pixmap()
pix.save(name '-' str(page.number) ".png")
print('success!')
CodePudding user response:
Try this:
from django.shortcuts import render, redirect
from app1.forms import ResumeUpload
from app1.models import UploadPdf
from app1.convert import convert_file
import os
def home(request):
if request.method == 'POST':
form = ResumeUpload(request.POST, request.FILES)
files = request.FILES.getlist('resumes')
if form.is_valid():
f = form.save()
f.user = request.user
f.save()
f_list = []
file_name = form.cleaned_data['resumes'].name
if files:
for i in files:
file_instance = UploadPdf(resumes=i)
f_list.append(file_instance.resumes.path)
file_instance.save()
for j in f_list:
convert_file(j)
else:
form = ResumeUpload()
return render(request, 'app1/home.html' , {'form': form})
CodePudding user response:
Finally found the solution! Try the below code:-
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class UserUploadModel(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE, null = True)
file = models.FileField(upload_to = 'file_uploads')
forms.py
from django import forms
from django.forms import ClearableFileInput
from app1.models import UserUploadModel
class UserUploadForm(forms.ModelForm):
class Meta:
model = UserUploadModel
fields = ['file']
widgets = {
'file' : ClearableFileInput(attrs = {'multiple': True}),
}
views.py
from django.shortcuts import render, redirect
from app1.forms import UserUploadForm
from app1.models import UserUploadModel
from app1.convert import convert_file
from app1.transfer import move_dir
import os
from project1 import settings
from django.contrib.auth.decorators import login_required
@login_required
def home(request):
if request.method == 'POST':
form = UserUploadForm(request.POST, request.FILES)
if form.is_valid():
f = form.save()
f.user = request.user
f.save()
files = request.FILES.getlist('file')
f_list = []
if files:
for i in files:
file_instance = UserUploadModel(file = i)
file_instance.save()
f_list.append(file_instance.file.path)
[convert(j) for j in f_list]
return redirect('app1-display')
else:
form = UserUploadForm()
return render(request, 'app1/home.html', {'form' : form})
home.html
{%block content%}
<form method="POST" enctype="multipart/form-data">
{%csrf_token%}
{{form.as_p}}
<input type="submit">
</form>
{%endblock content%}