Reputation: 27
so i´ve created a pdf document which should be saved in media/postings. the path is already in the function, but the pdf is not saved there but in the main project folder. what did i overlook here?
a little explanation for the def: the post is created and uploaded to the feed, while the image in the post is renamed and put into another folder. Also, a pdf is created, containing the posted material. How do i determine where this pdf goes on the server? i thought it is solved with the path_name and path_file but it doesn't work. thanks in advance.
def post_create(request):
if request.method == 'POST':
form = PostCreateForm(request.POST, request.FILES)
if form.is_valid():
form.cleaned_data['num_post'] = Post.objects.count() + 1
Post.objects.create(**form.cleaned_data)
else:
form = PostCreateForm()
return render(request, 'index.html', {'form': form})
def upload(request):
if request.method == 'POST':
image = request.FILES.get('image_upload')
caption = request.POST['caption']
num_post = Post.objects.count()+1
new_post = Post.objects.create(image=image, caption=caption, num_post=num_post)
new_post.save()
#create pdf
buffer = io.BytesIO()
x_start = 100
y_start = 10
folder_path = f"media/postings/post{num_post}.pdf"
folder_name = os.path.basename(folder_path)
print(folder_path)
p = canvas.Canvas(folder_name, pagesize=A4,)
#p == request.FILES.get('postings')
p.drawImage(ImageReader(image.file), x_start, y_start, width=420, preserveAspectRatio=True, mask='auto')
p.drawString(150, 650, new_post.caption)
p.drawString(100, 650, "caption:")
p.drawString(100, 170, str(new_post.created_at))
p.drawString(200, 700, str(num_post))
p.drawString(100, 700, "post no.:")
p.drawString(250, 700, "//")
p.drawString(100, 690, "===========================================================")
p.drawString(300, 700, str(new_post.id))
#p.showPage()
p.save()
buffer.seek(0)
#return render(request, '/', {'folder_name': folder_name, })
return redirect('/'), folder_path
else:
return redirect('/')
class Post(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
num_post = models.IntegerField(default=0)
image = models.ImageField(upload_to=post_images)
caption = models.TextField(max_length=300)
created_at = models.DateTimeField(auto_now_add=True)
number_of_likes = models.IntegerField(default=0)
like_list = models.TextField(max_length=300)
#likes_row = models.TextField(max_length=1000, default="hi")
number_of_dislikes = models.IntegerField(default=0)
interaction_count = models.IntegerField(default=0)
#downloads = models.FileField(upload_to='postings', blank=True, null=True)
@property
def filename(self):
return os.path.basename(self.image.name).split('.')[0]
def __str__(self):
return self.caption
Upvotes: 0
Views: 222
Reputation: 3717
os.path.basename returns only the right part of the path "postxxx.pdf" in your code and this is handed over to canvas...
folder_path = f"media/postings/post{num_post}.pdf"
folder_name = os.path.basename(folder_path)
p = canvas.Canvas(folder_name, pagesize=A4,)
Upvotes: 1