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.
Related
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.
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.
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 }}">
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.
In my blog app I want to display a list of blog posts and the first image connected to this post. Now I do it this way:
{% for image in entry.image_set.all|slice:"1" %}
<img src="{{ image.get_absolute_url }}">
{% endfor %}
Is there a template shortcut I don't know about, or maybe I should just write my own Manager?
Not any shorter, but you could use first:
{% with entry.image_set.all|first as image %}
<img src="{{ image.get_absolute_url }}">
{% endwith %}
Since Django 1.6 you can do
<img src="{{ entry.image_set.first.get_absolute_url }}">
You also can do:
entry.image_set.all.0 in your template.
I was having the following queryset I just want to retrieve first element from it in jinja template.
<QuerySet [<StudentDetails: StudentDetails object (1)>, <StudentDetails: StudentDetails object (2)>]>
Solution
{{ StudentDetails.first.name }}
StudentDetails is my model