I wonder how (if it is possible) load thumbnail for static files with easy-thumbnails package.
I tried:
<img src="{% thumbnail 'img/V.png' 50x0 %}" />
<img src="{% thumbnail static 'img/V.png' 50x50 %}" />
<img src="{% static thumbnail 'img/V.png' 50x50 %}" />
but nothing works.
The registered tag filters for easy-thumbnail package are not implemented in a way to render images from the static directory directly. Rather it expects an instance of Image/FileField model (Doc Reference). But you can implement your own filter, redirect the url to static directory and use it based on your needs.
Here, you can adopt one of the following strategies as well.
{% load static thumbnail %}
{% thumbnail image 50x50 crop="center" as thumb %}
{% if thumb %}
<img src="{{ STATIC_URL }}{{ thumb.url }}" width="{{ thumb.width }}" height="{{ thumb.height }}" alt="" />
{% else %}
<img src="{{ STATIC_URL }}img/V.png" alt=""/>
{% endif %}
or
{% load static thumbnail %}
{% thumbnail object.image|default:'{{ STATIC_URL }}img/V.png' 50x50 %}
or by using a custom tag filter to redirect the url of the image instance, if you use S3 bucket instance for static files.
settings.py
S3_ROOT_PATH = "<define your S3 hosting path>"
teamplatetags/s3static
from settings import STATIC_URL, S3_ROOT_PATH
from django import template
register = template.Library()
#register.filter
def s3_static_thumb(image_instance=None):
return "{s3_root_path}{static_path}{image_url}".format(s3_root_path=S3_ROOT_PATH, static_path=STATIC_URL, image_url=getattr(image_instance, "url")) if image_instance and hasattr(image_instance, "url") else None
finally, use that in your template:
{% load static thumbnail s3static %}
{% with image|s3_static_thumb as im_url %}
{% thumbnail image "720x306" crop="center" %}
{% if im %}
<img src="{{ im_url }}" width="{{ image.width }}" height="{{ image.height }}" alt=""/>
{% else %}
<img src="{{ STATIC_URL }}img/V.png" sizes="50x50" alt=""/>
{% endif %}
{% endwith %}
Related
Ive been working on a social media website where you can upload images and videos and follow other users.
I managed to upload and display uploaded files to the website.
I used FileField to load the image and video files, but when I implement it in my Template it shows both spaces, because there both using the same source url {{ source.file.url }}
models.py
class Post(models.Model):
title = models.CharField(max_length=150)
file = models.FileField(upload_to='uploads/%Y-%m-%d')
feeds.html
{% if post.file.url %}
<video class="video-context" width="500px" height="500px" controls>
<source src="{{ post.file.url }}" type="video/mp4">
</video>
{% endif %}
{% if post.file.url %}
<img class="image-context" src="{{ post.file.url }}" type="image/jpg" type="image/jpeg">
{% endif %}
heres a sreenshot empty video player over img
I tried the if endswith form:
{% if file.endswith .mp4 %}
<video class="video-context" width="500px" height="500px" controls>
<source src="{{ post.file.url }}" type="video/mp4">
</video>
{% endif %}
{% if file.endswith .jpg/jpeg %}
<img class="image-context" src="{{ post.file.url }}">
{% endif %}
But it didn't work.
How can I just display the file thats uploaded. How can I make the {{ post.file.url }} unique for image and video, so its easier to difference?
How is it possible to difference the upload types, but still using FileField?
Django templates filter "endswith" doesn't give you the extension of the file in your filefield. Instead, it gives the last character of an object, so in your case, it will be returning 4 if the file is a video type or it will return g if file is an image.
You want to get the extension of the uploaded and unfortunately, there is no filter for that in Django templates.
You need to create a function in your models that will return your file-type/extension.
Models.py
import os
class Post(models.Model):
title = models.CharField(max_length=150)
file = models.FileField(upload_to='uploads/%Y-%m-%d')
def extension(self):
title, extension = os.path.splitext(self.file.name)
return extension
Then in your templates, you can get the extension through this code...
{% for post in posts %}
{% if post.extension == '.mp4'%}
<video class="video-context" width="500px" height="500px" controls>
<source src="{{ post.file.url }}" type="video/mp4">
</video>
{% elif post.extension == '.jpg' or post.extension == '.jpeg' %}
<img class="image-context" src="{{ post.file.url }}">
{% endif %}
{% endfor %}
You can have all sorts of methods on a model to make your life and project easy.
I am trying to implement blog app with django.In home page there will be list of post."post.author.profile.image" is a path to load image from database.If
"post.author.profile.image" is None i need to load an alternative image and if it exist it should load the image from database.So i tried the following code:
def homepage(request):
post= Post.objects.all().order_by('-date')
return render(request,'layout.html',{'posts':post})
layout.html
{% for post in posts %}
<div class="list">
<div class="con">
{% if "post.author.profile.image.url" is None %}
<img src="{% static 'images/b.png' %}" class='logo3'/>
{% else %}
<img src="{{ post.author.profile.image.url }}" class='logo3'/>
{% endif %}
</div>
</div>
{% endfor %}
After running the server if i click on inspect the path in src of image tag is media/None.The code under if is not even running.Whats problem in my code?
How about checking if the image exists?
{% if post.author.profile.image %}
<img src="{{ post.author.profile.image.url }}" class='logo3'/>
{% else %}
<img src="{% static 'images/b.png' %}" class='logo3'/>
{% endif %}
Try this
{% if post.author.profile.image.url %}
<img src="{{ post.author.profile.image.url }}" class='logo3'/>
{% else %}
<img src="{% static 'images/b.png' %}" class='logo3'/>
{% endif %}
Here
{% if "post.author.profile.image.url" is None %}
you're testing if the literal string "post.author.profile.image.url" is None - which it's garanteed to be false, since a literal string is never None.
You want the variable itself:
{% if post.author.profile.image.url is None %}
{% if post.author.profile.image == 'null' %}
<img src="{% static 'images\b.jpg' %}" class="logo3">
{% else %}
<img src="{{post.author.profile.image.url}}" class="logo3">
{% endif %}
I still wonder how this code worked .I tried this before but at that time it was not working may be i would had done some other mistake.But this code is working.
I have two templates that go together, one inside the other. The inner one has an icon that needs to change according to the content of the parent template.
I've tried to pass the icon path using a variable:
src="{% url 'main_bar_icon' %}">
and I added this line of code in the parent template:
{% with main_bar_icon='../static/dist/img/logout-icon.svg' %}
{% include 'main_bar.html' %}
{% endwith %}
So, this is my inner template:
{% block main_bar %}
<a href="">
<img class="app-main-bar-icon"
src="{% url 'main_bar_icon' %}">
</a>
{% endblock main_bar %}
And this is my parent template:
{% block content %}
{% with main_bar_icon='/dist/img/logout-icon.svg' %}
{% include 'main_bar.html' %}
{% endwith %}
{% endblock content%}
In the browser I get this:
<img class="app-main-bar-icon" src(unknown) alt="icon">
Unfortunately, the {% url ... %} templatetag, can be only used to retrieve urls for the views defined in the urlpatterns.
For your needs, you will need either:
use plain variable, since you already assign path to the variable, simply: <img class="app-main-bar-icon" src="{{ main_bar_icon }}">
or, for more future proof solution, you can configure django static files and use {% with main_bar_icon='dist/img/logout-icon.svg' %} and then <img class="app-main-bar-icon" src="{% static main_bar_icon %}">
I am using Django views to display some images in a folder. The program is able to search for the files but not able to display, there is a 404 error for all the images.
views.py
import os
from datetime import datetime
now = datetime.now()
image_now = now.strftime("%d_%m_%Y")
image_list=[]
app_static_dir = os.path.join(os.path.join(os.path.join(os.path.join(settings.BASE_DIR,'index'),'static'),'{}'.format(image_now)),'demo')
for file in os.listdir(app_static_dir):
if file.endswith("1_plate.jpg"):
image_list.append(file)
html
{% for file in image_list %}
<img src="{{ file }}" alt="">
{% endfor %}
Use the {% static %} template tag to prepend the correct static root directory.
{% load static %}
{% for file in image_list %}
<img src="{% static file %}" alt="" />
{% endfor %}
You also have to configure the staticfiles app correctly as described in the docs:
Managing static files (e.g. images, JavaScript, CSS)
I have question how to load media in my template
I use sorl-thumbnails to create thumbnails
and I want to show the picture in template
Here is my settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT= os.path.join(BASE_DIR, 'media')
MEDIAFILES_DIRS = (
("cache", os.path.join(MEDIA_ROOT,'cache')),
)
my templates : english/maininfo.html
{% load staticfiles %}
{% load thumbnail %}
<ul id="menu">
<li>
{% thumbnail "http://www.test/img/logo.png" "100x80" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}
</li>
there is error :
The current URL, media/cache/d8/fe/d8fe70d567ce1a03769de7db30634696.jpg, didn't match any of these.
But I really have this directory
this is the project structure
and my views.py
def maininfo(request):
responseContext = {'lang':request.LANGUAGE_CODE,}
return render(request, 'english/maininfo.html',responseContext)
How to load media files in templates??
Please help me! Thank you very much