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
Related
I just want to display profile picture.Img upload successfully but not showing the img.But if i try in admin panel then show me img. Here is my index.html code
{% extends 'Login_app/base.html' %}
{% block body_block %}
{% if user.is_authenticated %}
<p>Hi {{ user_basic_info.username }},</p>
<p>Your Email {{ user_basic_info.email }}</p>
<p> Facebook Profile </p>
<img src="/media/{{ user_more_info.profile_pic }}" width="200px">
{% else %}
<div class="alert alert-primary"></div>You are not logged in!!</div>
{% endif %}
{% endblock %}
Here is my setting.py media setting
import os
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # This is a tuple
MEDIA_URL = '/media/'
Here the ss of website
You have to add .url after the profile picture.
Try This:
<img src="/media/{{ user_more_info.profile_pic.url }}" width="200px">
OR
In Models.py define a get_absolute_url() method to join the media directory:
def get_absolute_image(self):
return os.path.join('/media', self.image.name)
And in templates do this:
{% for post in posts %}
<img src="{{ post.get_absolute_image }}" width="200px">
{% endfor %}
p.s.- here post is model name
django setting
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
django views
def single_article(request, page, slug):
article = get_object_or_404(Article, slug=slug)
return render(request, "article/single_article.html", locals())
django template
<`enter code here`img class="d-block w-100 img-responsive" style="height: 100%" src="{{ article.picture.url }}" alt="{{ article.title }}"
> Blockquote
you don't have to pass /media/ or use .url to fetch it from db
{% extends 'Login_app/base.html' %}
{% block body_block %}
{% if user.is_authenticated %}
<p>Hi {{ user_basic_info.username }},</p>
<p>Your Email {{ user_basic_info.email }}</p>
<p> Facebook Profile </p>
<img src="{{ user_more_info.profile_pic.url }}" width="200px">
{% else %}
<div class="alert alert-primary"></div>You are not logged in!!</div>
{% endif %}
{% endblock %}
this will work if still image is not loadin add you views
Try to add this code to the project's urls.py:
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
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 %}
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 am facing problem showing image file in the django ecommerce project.Can anyone help in this issue?
home.html
{% for product in products %}
<div class='col-sm-4'>
<div class="thumbnail">
{% if product.productimage_set.all %}
{% for item in product.productimage_set.all %}
<img class='img-responsive' src="{{ MEDIA_URL }}{{ item.image}}" />
{% endfor %}
{% else %}
<img class='img-responsive' src="{% static "img/placeholder.svg" %}" />
{% endif %}
<div class="caption">
<a href='{{ product.get_absolute_url }}'> <h3>{{ product.title }}</h3></a>
<p>{{ product.description|truncatewords:15}}</p>
<p>View Button</p>
</div>
</div>
</div> {% endfor %}
model.py
class ProductImage(models.Model):
product = models.ForeignKey(Product)
image = models.ImageField(upload_to='products/images/')
featured = models.BooleanField(default=False)
thumbnail = models.BooleanField(default=False)
active = models.BooleanField(default=True)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.product.title
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","media")
<img class='img-responsive' src="{{ MEDIA_URL }}{{ item.image.url }}" />
or
<img class='img-responsive' src="{{ item.image.url }}" />
and in main urls.py
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
UPDATE: I solved this by adding urls
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),)
I use sorl-thumbnail to create image.
It really create the image in directory,but it can't get the path (firebug said 404 not found)
please help me to check.
Thank you very much.
settings.py
THUMBNAIL_DEBUG = True
MEDIA_URL = '/media/'
MEDIA_ROOT= os.path.join(BASE_DIR, 'media')
template: menu.html
{% load thumbnail %}
{% thumbnail "https://www.google.com.tw/images/srpr/logo11w.png" "100x80" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}">
{% endthumbnail %}