Loading external Images with Django
See original GitHub issueHello! Currently on a small project that uses WeasyPrint for generating a PDF based on a Jinja2 HTML template. Here is the template:
<div class="details">
<img src="{{ image }}" style="height: auto; width: 50px"/>
<img src="http://place-hold.it/300" alt={{ id }} width="120" height="90"/>
<div class="img-div" style="background-image: url('{{ image }}');">
</div>
</div>
Im trying go get it to work with three different methods. {{ image }}
has the content of http://place-hold.it/300
. Im just wondering why this wouldnt work? Its just an image from an external source, no auth needed or anything. Could it be CORS in the django project?
I have created wrapper functions:
def render_template_to_pdf(template_name, output_file: IO, context: dict):
# Render mail template using Django's template rendering
template: Template = get_template(template_name)
template_path = template.origin.name
html = template.render(context)
render_html_to_pdf(html, output_file, base_url=os.path.dirname(template_path))
def render_html_to_pdf(html: str, output_file: IO, base_url=None):
import weasyprint
pdf = weasyprint.HTML(string=html, base_url=base_url)
pdf.write_pdf(target=output_file)
def generate_pdf(output_file: IO):
data = {
"id": 2
"image": "http://place-hold.it/300"
}
render_template_to_pdf(
"test.html",
output_file=output_file,
context=data
)
output_file.seek(0)
# View function that uses bove generator
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename="test.pdf"'
with NamedTemporaryFile() as output_file:
generate_pdf(data, output_file)
response.write(output_file.read())
return response
The PDF gets created perfectly with the rest of the information that is just text and tables. Local images ALSO work, but we will have images served from an external source.
Any pointers on how to get this to work? I am really lost 😦
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
how to show external media in template in django
The images are external and not in your db, you are just storing the URL to the images. And you are using a...
Read more >Python | Uploading images in Django - GeeksforGeeks
Here upload_to will specify, to which directory the images should reside, by default django creates the directory under media directory which ...
Read more >Django File (and Image) Uploads Tutorial | LearnDjango.com
This tutorial shows how to implement file and then image uploading with Django. We'll build a basic Instagram clone.
Read more >How to manage static files (e.g. images, JavaScript, CSS)
Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS . · In your settings file, define STATIC_URL , for example: · In your...
Read more >Upload Images To Django - Django Wednesdays #38
In this video I'll show you how add the ability for users to upload images to your Django App. Uploading images to Django...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this can be closed then. After installing/updating GDK-Pixbuf, these images work well from different sources, without using
django-weasypring
. just the normal one 😃Thank you so much for your help and patience with me. WeasyPrint is awesome already and I hope you will keep improving it!
Handling JPG files requires the GDK-Pixbuf package to be installed. PNG and SVG already work, without GDK-Pixbuf.