Using Django Templates to send an email - django

I have a Django template that I'm using to send emails. I'm using Sorl thumbnail to generate a thumbnail to be used in the email.
{% thumbnail event.image '90x100' crop=True as im %}
<img class="float-center"
src="{{ im.url }}" alt="">
{% endthumbnail %}
The issue is the src is an absolute path on the filesystem. I would like to prepend the im.url with my server's web address. I tried a few ways of concatenating strings but nothing seemed to stick.

Related

Django Rendering images in Email templates

I have created the email template as shown below
{% load staticfiles %}
<h1 style="color:red; background-image:url('../images/contact.jfif')">Welcome {{contact_name}}!</h1><br>
Your username is {{ contact_email }} and your content is {{ form_content }}.
<img src="{{ request.get_host }}{% static 'images/logo.png' %}" alt="ddddd" />
- xyz
</div>
But the image is not getting displayed in the email and showing error
Can anyone help me by providing the solution
You are giving the images url as relative to your own website.
But then you are trying to fetch it on google with that relative url.
It will definitely show the error as image will not be found on their server.
Try giving absolute url for the image.
I think it should work fine then.

Sorl thumbnail not working [Errno 2] No such file or directory

I'm using sorl-thumbnail 12.2
{% load thumbnail %}
{% for image in citem.get_images %}
<div class="image-block">
{% thumbnail image.img.path '180x180' as thumb %}
{% thumbnail image.img.path '640x640' as medium %}
<a data-lightbox="i-{{image.id}}" href="{{medium.url}}" title="{{citem.display_plant_smart}}">
<img class="img-rounded img-responsive bound-img-big" src="{{ thumb.url }}" alt="{{citem.display_plant_smart}}" style="padding-bottom: 1px"/>
</a>
{% endthumbnail %}
Usually this code was working, for some unknown reason it stops working.
Now I'm getting
[Errno 2] No such file or directory
Even the file exists on disk. When trying to run sorl migrations I get this:
django.db.utils.ProgrammingError: relation "thumbnail_kvstore" already exists
I'm using MAC and the same is happening in linux as well. I've already fake the migration with no success. The images are there in the disk.

FileField/ImageFIeld URL isn't giving me a HTTP URL

I have this code, where partner.logo is an ImageField in the Partner model.
{% for m in matches %}
<img src="{{ m.partner.logo }}" alt="" />
<h2>{{ m.partner.name }}</h2>
<p>{{ m.reasons }}</p>
<p>{{ m.partner.profile }}</p>
{% endfor %}
Now, the Django documentation says if you go m.partner.logo.url, you'll get a URL to the file. However, all I got was
<img src="/Users/shinichi/Dropbox/source/blastoise/uploads/road-sky-clouds-cloudy.jpg" alt="" />
and that actually is the same string that I'd get from m.partner.logo. This isn't a URL, and it's not helping! I was expecting something like http://127.0.0.1:8000/uploads/road-sky-clouds-cloudy.jpg. Am I missing something?
You probably have some problems with the MEDIA_URL and the MEDIA_ROOT on the settings.py (please take a look here) as pointed by #domino. I've written an example to you here. There is one important detail too when referencing the logo URL, you probably will need to do something like this. I mean:
{% for partner in partners %}
<img src="{{ partner.logo.url }}" alt="" />
<h2>{{ partner.logo }}</h2>
{% endfor %}
Remember to add these lines on your urls.py to help you with serving the media resources on development, it always helps a bit with.
Hope that it can help you mate.
It really depends on what you've set your MEDIA_ROOT to in proj/settings.py file. In your models.py file, you can designate the upload_to argument in the FileField. Here's the documentation on that.
You can designate other places. The most popular is Amazon S3. I set my template to read
<img src="https://s3-us-west-2.amazonaws.com/fsbo-facebook/{{ house.photo }}" alt="{{ house.title }}">
Hope this helps.

how to load image in the size specified in django template?

Suppose, I have 20 images of size 1600 X 900 in a page. How do I load the images in the size that I specify on a template? In the css I can do it. But I want to change the actual size of the image, so that when clicked on the particular image, it will load the original image with its original size. Is there any way that I can do it? I tried using easy_thumbnails and it was great, until it gave me problems when I deployed it using the apache server. Any help will be deeply appreciated. Thank you!
EDIT:
.html:
{% for Status in status %}
<p class="user">{{ Status.creator.get_full_name }}</p>
{% if Status.image %}
<div class="image_image">
<center>
<img src="{{ MEDIA_URL }}{{Status.image}}" width=300px />
</center>
</div>
<p class="status_image">{{Status}}</p>
<span class="clear"></span>
<hr>
{% else %}
<p class="status_status">{{Status}}</p>
<span class="clear_right"></span>
<hr>
{% endif %}
{% endfor %}
Try to use sorl thumbnail, it even supports Amazon S3 Server.
pip install sorl-thumbnail
To Installed apps:
'sorl.thumbnail',
add {% load thumbnail %} to your template.
{% thumbnail firma.firma_logo "130x110" format="PNG" as im %}
<div class="logo" style="background-image:url('{{ im.url }}')"></div>
{% endthumbnail %}
The make image format PNG is important, it converts into JPEG by the default. And if the user uploads file in transparent format, it sucks.

How do you display image objects in a Django template using a custom template tag? (coding included)

I have the following code that fails to display object images. But displays normal images fine.
My Model
class News(models.Model):
title-----------
image = models.ImageField(upload_to='images')
body------------
Template tag coding
from django import template
register = template.Library()
from ----.models import ---
def funct(num):
myobjects = News.objects.all()[:num]
return {'objects': myobjects}
register.inclusion_tag('news/template.html')(funct)
template coding
{% for object in objects %}
<li>{{ object.title }}</li>
<li><img src="{{ MEDIA_URL }}images/{{ object.image }}" alt="image" /></li>
<li>{{ object.body }}</p></li>
{% endfor %}
This code outputs all the variable information such as title and body in a list however it does not display the associated image. I have tried numerous variations on this code with no success. This is strange because when an image is called from the image folder in the following manner
<img src="{{ MEDIA_URL }}images/star.jpg" />
Everything works fine. The problems occur when its a model image being called.
Any help fixing this issue is much appreciated
This has nothing to do with the custom template tag. If you looked at the source for the rendered page, you would see that {{ object.image }} does not output the URL for the image. You need to use {{ object.image.url }}