2
我使用this pdf生成的第三方应用程序。是否可以将django中的视图生成的PDF附加到电子邮件中?
在我看来,我有这个代码创建PDF:
class artwork_catalog(PDFTemplateView):
template_name = "someapp/sometemplate.html"
def get_context_data(self, **kwargs):
return super(artwork_catalog, self).get_context_data(
pagesize="A4",
title="My Title" + datetime.datetime.now().strftime('%m/%Y'),
artworks=ArtWork.objects.filter(public=True).order_by('year'),
**kwargs
)
我有以下网址为我的观点:
url(
r"^mypdf.pdf$",
artwork_catalog.as_view()
),
这样我就可以在localhost:8000/mypdf.pdf
访问我的PDF文件。 现在我想发送这个PDF作为每封电子邮件的附件。我创建了一个视图,但我无法使其工作:
def order_catalog(request):
if request.method == 'POST':
form = OrderCatalogForm(request.POST)
if form.is_valid():
subject = 'Test'
from_email = '[email protected]'
message = 'TESTTESTTESTTEST'
recipient = form.cleaned_data['email']
pdf = artwork_catalog # here I'd like to insert my Pdf
try:
mail = EmailMessage(subject, message, from_email, [recipient])
mail.attach("MyPdf.pdf", pdf, "application/pdf")
mail.send()
except:
return HttpResponse('Error')
return redirect('/somewhere/')
else:
form = OrderCatalogForm()
return render(request, 'someapp/order_catalog.html', {'form': form})
任何提示?最好的问候曼努埃尔