How to use local default image for django-gravatar? - django

I am using django-gravatar and I am wondering how to display a local image to use as a default image. Is there a way to do so?
I have read in the docs to use GRAVATAR_DEFAULT_IMAGE, but it does not seem to work specifying the path to the image within the static/ directory. Is it only for online images from other websites?
Thanks!
EDIT
I am using the following configuration:
STATIC_URL = '/static/'
GRAVATAR_DEFAULT_IMAGE = STATIC_URL + 'img/StartMyProjects_100.png'
Hack finally used to solve the problem:
<img class="media-object" src="
{% if profile.gravatar_email %}
{% gravatar_for_email profile.gravatar_email %}
{% else %}
/static/img/StartMyProjects_100.png
{% endif %}"
alt="{{ profile.full_name }}">

To solve the problem, as stated in the edited question, I actually used an if-else (in Django). Maybe someone knows a better way to do so, but this worked for me!
<img class="media-object" src="
{% if profile.gravatar_email %}
{% gravatar_for_email profile.gravatar_email %}
{% else %}
/static/img/StartMyProjects_100.png
{% endif %}"
alt="{{ profile.full_name }}">

Related

Django: Iterate through static files in HTML, cannot use filter

I followed this answer.
Iterate through a static image folder in django
But I'm not sure why it's giving me the following error
django.template.exceptions.TemplateSyntaxError: Invalid filter: 'file'
I have
{% with 'images/'|file as image_static %}
<img src="{% static image_static %}" alt="">
<a class="gallery-one__link img-popup" href="{% static image_static %}"><i class="tripo-icon-plus-symbol"></i></a>
{% endwith %}
I did pass the context dict correctly in views, I think, because I saw this in my traceback:
context_dict = {}
files = os.listdir(os.path.join(settings.STATIC_ROOT, "blog/images/gallery"))
context_dict['files'] = files
I also tried {% load crispy_forms_filters %} at the start of the HTML but it's not making a difference. I'm quite new to this, so I'm probably doing something stupid, and I cannot find any docs referring to this specific instance
The problem is that the answer you based your code on is broken. That answer has a comment from someone who tried it and got the same error that you did.
Try this:
{% for file in files %}
<img src="{% static file %}" alt="">
<a class="gallery-one__link img-popup" href="{% static file %}"><i class="tripo-icon-plus-symbol"></i></a>
{% endfor %}

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.

django applying a style class based on a conditional

I am displaying a list of images.
If the user has uploaded an image, I want to keep its opacity 0.5 and in the list of images, the images uploaded by others should have full opacity.
I have done it as follows, is there a better way to do it??
{% if request.user == obj.shared_by %}
<div class="item-image" style="opacity:0.5;filter:alpha(opacity=50);">
{% else %}
<div class="item-image">
{% endif %}
......Some code here....
</div>
Thanks!
I normally go for:
<div class="item-image{% if foo %} own-image{% endif %}">...</div>
but switching out the entire div tag may be more readable.
Either way I'd do the styling with another class, not with inline css.
I have added class on if condition by this way....
<li class="nav-item {% if app_url == '/' %} active{% endif %}">

Django TemplateSyntax Error with Sorl-thumbnail

I'm trying to get Sorl-thumbnail running on my staging server, but I'm running into a TemplateSyntaxError which is throwing me since the app works fine on localhost.
The error is coming in at {% endthumbnail %}
TemplateSyntaxError at /home/
Invalid block tag: 'endthumbnail', expected 'endif'
Any help would be greatly appreciated. Thanks!
{% load thumbnail %}
{% if picture.photo_medium %}
<img src="{{AWS_URL}}{{picture.photo_medium}}" class="imagepage" width="400" height="300">
{% else %}
{% if picture.photo_large|is_portrait %}
<div class="portrait">
{% thumbnail picture.photo_large "400" crop="center" as im %}
<img src="{{AWS_URL}}{{ im }}">
</div>
{% else %}
<div class="landscape">
{% thumbnail picture.photo_large "400" crop="center" as im %}
<img src="{{AWS_URL}}{{ im }}">
</div>
{% endif %}
{% endif %}
It is likely that you have an older version of sorl-thumbnail installed on your localhost than is installed on your staging server. The endthumbnail tag was added relatively recently as part of a major rewrite.
If you find that you need to upgrade you may find the setting THUMBNAIL-DEBUG helpful for tracking down other problems.
I might be wrong, but I don't think you need the {% endthumbnail %} tag.
The problem can also be with loading template tags.
I was doing {% load thumbnail %} in base html.
When I call below code in inherited html, got same error.
{% thumbnail service_type.pic.image "100x100" crop="center" as im %}
<img .....>
{% endthumbnail %}
See this discussion about loading template tags in base.html
I just ran into this problem in using SORL Thumbnail in Mezzanine. Apparently Mezzanine loads it's own thumbnailer, so if you {% load thumbnail mezzanine_tags %}, mezzanine's thumbnail takes over from SORL's Thumbnail tag. However, if you reverse it {% load mezzanine_tags thumbnail %}, it works fine.
Lesson Learned: Make sure other libraries you're using aren't inadvertently taking over, and just to be safe maybe load thumbnail last.