When i run my code with the local server my images are not visualized, there are images icons but not the image itself.
settings.py:
MEDIA_URL = '/images/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
my template tienda.py:
{% extends 'tienda/index.html'%}
{%load static%}
{% block content %}
<div class="row">
{% for product in products %}
<div class="col-lg-4">
<img src="{{product.imageURL}}" alt="" class="thumbnail" >
<div class="box-element product">
<h6><strong>{{product.name}}</strong></h6>
<hr>
<button data-product={{product.id}} data-action="add" class="btn btn-outline-secondary add-btn update-cart">Agregar al carrito</button>
<a class="btn btn-outline-success" href="">Ver</a>
<h4 style="display: inline-block; float: right;"><strong>${{product.price|floatformat:2}}</strong></h4>
</div>
</div>
{%endfor%}
</div>
{% endblock %}
my model Product:
class Product(models.Model):
name = models.CharField(max_length=200, null=True)
price = models.FloatField()
digital = models.BooleanField(default=False, null=True, blank = False)
images = models.ImageField(null=True, blank=True)
Related
I am having trouble on how to display an image on html? i just want to display all image that saves on my database
{% for banner in banners %}
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img class="d-block w-100" src="{{ banner.banner }}" style="width:100%">
<div class="text">{{banner.title}}</div>
</div>
{% endfor %}
model
class Banner(models.Model):
banner = models.ImageField(upload_to='image/', null=True, blank=True)
views
def homepage(request):
banners = Banner.objects.all()
return render(request, 'mysite/homepage.html', {'banners': banners})
result
enter image description here
Use the 'url' property of the ImageField.
Try this:
{{ banner.banner.url }}
models.py
class Banner(models.Model):
banner = models.ImageField(upload_to='image/', null=True, blank=True)
def get_banner_url(self):
return self.banner.url
html
{% for banner in banners %}
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img class="d-block w-100" src="{{ banner.get_banner_url }}" style="width:100%">
<div class="text">{{banner.title}}</div>
</div>
{% endfor %}
for be sure that your URL is coming try this(just for test):
<div class="text">{{banner.get_banner_url }}</div>
if you can't see the URL that means it is about your media tags
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
main urls.py
urlpatterns = [
#your paths
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
I am trying to render images from my database to carousel in Django. I have a model which contains url fields to store images url but when I use them in (template.html) my carousel img src, it only renders 1st image but not others even though I have three images url stored in database. To understand it better here is my code.
models.py
from django.db import models
# Create your models here.
class CaraouselData(models.Model):
title = models.CharField(max_length=100, null=False)
description = models.TextField(max_length=200, null=True)
image = models.URLField(null = False, default=True)
def __str__(self):
return self.title
views.py
def home(request):
caraousel_data = CaraouselData.objects.all()
return render(request, 'index.html', context={
'caraousel_data': caraousel_data,
})
template.js
{% block content %}
{# Caraousel Data Section#}
<div class="container-fluid p-0">
<div id="carouselExampleCaptions" class="carousel slide" data-bs-ride="carousel">
<div class="carousel-indicators">
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="1" aria-label="Slide 2"></button>
<button type="button" data-bs-target="#carouselExampleCaptions" data-bs-slide-to="2" aria-label="Slide 3"></button>
</div>
{# images fetching from models #}
<div class="carousel-inner">
{% for item in caraousel_data %}
<div class="carousel-item {% if forloop.counter0 == 0 %} active" {% endif %}>
<img src="{{ item.image }}" width="600" height="670" class="d-block w-100" alt="...">
<div class="carousel-caption d-none d-md-block">
<h5>{{ item.title }}</h5>
<p>{{ item.description }}</p>
</div>
</div>
{% endfor %}
{% endblock %}
I think you have not installed the pillow library.🤔
To handle images you must install them first.
Im working on my django project. The home page of my project is to show all of my post and category-based post on the bottom after the first part. But my code isnt working and i dont know why. Please help me, im a django noob
#the word Opini is one of my post category
{% for post in blog_posts %}
<div class="card d-inline-flex m-1" style="width: 18rem;">
<a href="{% url 'post-detail' post.pk %}" >
<img class="card-img-top postimg" src="{{ post.post_img.url }}" alt="Card image cap">
</a>
<h3>{{post.category}}</h3>
<div class="card-body">
<h5 class="card-text text-center">{{ post.post_title }}</h5>
<p class="card-text">{{post.post_content|truncatechars:75|safe }}</p>
</div>
</div>
{% endfor %}
<h1> Opini </h1>
{% for post in blog_posts %}
{% if post.category == "Opini" %}
<div class="card d-inline-flex m-1" style="width: 18rem;">
<a href="{% url 'post-detail' post.pk %}" >
<img class="card-img-top postimg" src="{{ post.post_img.url }}" alt="Card image cap">
</a>
<h3>{{post.category}}</h3>
<div class="card-body">
<h5 class="card-text text-center">{{ post.post_title }}</h5>
<p class="card-text">{{post.post_content|truncatechars:75|safe }}</p>
</div>
</div>
{% endif %}
{% endfor %}
{% endfor %}
My Models
class Category(models.Model):
name = models.CharField(max_length=100, unique=True)
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('post_by_category', args=[self.name])
class Post(models.Model):
post_title = models.CharField(max_length=50)
post_img = models.ImageField(upload_to='postImage/')
post_content = RichTextField(blank= True, null = True)
category = models.ForeignKey(Category , on_delete=models.CASCADE)
post_date = models.DateTimeField(auto_now_add=True)
post_author = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
ordering = ["-post_date"]
def __str__(self):
return f'{self.post_title}'
When i run the code, only the first page works, when it comes to the second part, nothing appeared
If "Opini" is the category name then you should compare it like this:
{% if post.category.name == "Opini" %}
WebSite
Admin Panel
First of all sorry for bad english. But i cant see my images on my website. I can see titles descriptions. Can you help me. I checked page resources it links my images and there is no problem.
thats exactly where my photos are
page resources
settings.py
STATIC_URL = '/static/'
MEDIA_URL = '/uploads/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')
template
<ul class="slides-container">
{% for rs in sliderdata %}
<li class="text-center">
<img src="{{ rs.image.url }}" alt="">
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="m-b-20"><strong>Hoşgeldiniz <br> Site İsmi</strong></h1>
<p class="m-b-40">{{ rs.title}} <br> {{ rs.description }}</p>
<p><a class="btn hvr-hover" href="#">İncele</a></p>
</div>
</div>
</div>
</li>
{% endfor %}
views.py
def index(request):
setting = Setting.objects.get(pk=1)
sliderdata = Slider.objects.all()[:3]
category = Category.objects.all()
context={'setting':setting, 'page': 'home','category':category,'sliderdata':sliderdata}
models.py
class Slider(models.Model):
Status = (
('True', 'Evet'),
('False', 'Hayır'),
)
title = models.CharField(max_length=150)
description=models.CharField(max_length=255)
image = models.ImageField(upload_to='images/', blank=True)
create_at = models.DateTimeField(auto_now_add=True)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
It is because you added your images not in your templates. You should add
<img src="{{ rs.image.url }}">
I included the images in my database and store the images in the media directory. When I access my data from my database all information is loaded but my images are not showing. Can someone help me out?
This is my index.html file:
{% block body %}
{% load static %}
<div class="container">
<h1>Men Fashion</h1>
<div id="demo" class="carousel slide my-3" data-ride="carousel">
<ul class="carousel-indicators">
<li data-target="#demo" data-slide-to="0" class="active"></li>
{% for i in range %}
<li data-target="#demo" data-slide-to="{{i}}" ></li>
{% endfor %}
</ul>
<!--Slideshow starts here -->
<div class="container carousel-inner no-padding">
<div class="carousel-item active">
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="card" style="width: 18rem;">
{% load static %}
<img src='/media/{{product.0.image}}' class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{product.0.product_name}}</h5>
<p class="card-text">{{product.0.desc}}</p>
Add to Cart
</div>
</div>
</div>
{% for i in product|slice:"1:"%}
<div class="col-xs-3 col-sm-3 col-md-3">
<div class="card" style="width: 18rem;">
<img src='/media/{{i.image}}' class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{i.product_name}}</h5>
<p class="card-text">{{i.desc}}</p>
Add To Cart
</div>
</div>
</div>
{% if forloop.counter|divisibleby:3 and forloop.counter > 0 and not forloop.last %}
</div><div class="carousel-item">
{% endif %}
{% endfor %}
</div>
</div>
</div>
<!-- left and right controls for the slide -->
<a class="carousel-control-prev" href="#demo" data-slide="prev">
<span class="carousel-control-prev-icon"></span>
</a>
<a class="carousel-control-next" href="#demo" data-slide="next">
<span class="carousel-control-next-icon"></span>
</a>
{% endblock %}
</div>
This is my models.py file which I included in my project:
from django.db import models
# Create your models here.
class Product(models.Model):
product_id = models.AutoField
product_name = models.CharField(max_length=50)
category = models.CharField(max_length=50, default="")
subcategory = models.CharField(max_length=50, default="")
price = models.IntegerField(default=0)
desc = models.CharField(max_length=300)
pub_date = models.DateField()
image = models.ImageField(upload_to='shop/images', default="")
def __str__(self):
return self.product_name
This is my setting.py file:
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
I do not know to much about this but..
Please have a look here:
Django image src not found
Similar problem to you, corrected by code.
In your model.py code, you say:
image = models.ImageField(upload_to='shop/images', default="")
and in your settings.py you have:
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/
I do not see where you declare a path to your images.
Try this code for your settings.py and see what works for you.
from django.core.files.storage import FileSystemStorage
from django.conf import settings
image_storage = FileSystemStorage(
# Physical file location ROOT
location=u'{0}/my_sell/'.format(settings.MEDIA_ROOT),
# Url for file
base_url=u'{0}my_sell/'.format(settings.MEDIA_URL),
)
def image_directory_path(instance, filename):
# file will be uploaded to MEDIA_ROOT/my_sell/picture/<filename>
return u'picture/{0}'.format(, filename)
class Goods(models.Model):
pic = models.ImageField(upload_to=image_directory_path, storage=image_storage)
For more information please see:
Django image src not found
Hope it helps.
First off, you should double-check where your images are actually being uploaded to.
Based on the ImageField, I'm guessing you want to store your product images to media/shop/images. In your template, however, you call /media/{{product.0.image}}, which will look for images in media/media/shop/images.
Secondly, remember that searches for the path to the image, not just the image itself. So try this:
<img src="{{product.0.image.url }}" class="card-img-top" alt="...">