I'm trying to upload a picture from ImageField in template, but it is not displayed. Code and screenshots will explain better.
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = '/media/veracrypt1/django/pycaba/pycaba/media/'
models.py
image = models.ImageField()
views.py
def thread(request, boardname, thread_id):
board = boardname
thread = thread_id
op_post = get_object_or_404(Posts, board=board, id=thread_id)
return render(request, 'board/thread.html', {'op_post':op_post})
thread.html
{% extends 'board/base.html' %}
{% block content %}
<div class="op_post">
<img src="{{op_post.image.url}}">
</div>
{% endblock content %}
thread.html source in browser
enter image description here
What Django version are you using? Have you included static file urls in you url patterns?
Related
So I've been trying to get the images uploaded through admin interface to render on a template but for some odd reason it just wont render the images. It renders any other type of data but NOT IMAGES.
From Settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
From appvew.py:
class ProjectsListView(ListView):
model = Projects
context_object_name = 'projects'
template_name = 'myportfolio/portfolio.html'
appurls.py:
urlpatterns =[
path('home/', views.ProjectsListView.as_view()),
# path('projects/', )
]
html template:
{% load static %}
</head>
<body>
<h1><p>This is my portfolio</p></h1>
{% for project in projects %}
<img src="{{project.screenshot}}" alt="No image found">
{% endfor %}
</body>
</html>
Error message on the terminal:
Not Found: /portfolio/home/projects/images/shutterstock_253580635_square.jpg
Not Found: /portfolio/home/projects/images/mountains.jpg
[25/Mar/2022 14:16:06] ←[33m"GET /portfolio/home/projects/images/shutterstock_253580635_square.jpg HTTP/1.1" 404 2560←[0m
[25/Mar/2022 14:16:06] ←[33m"GET /portfolio/home/projects/images/mountains.jpg HTTP/1.1" 404 2500←[0m
output :( :
I verified an old project of mine with pretty much the same set up and it works. This one on the other hand, only renders the name of the objects on other properties but not the images
Try this
<img src="{{project.screenshot.url}}" alt="No image found">
https://docs.djangoproject.com/en/4.0/topics/files/
I seem do not understand what i am doing wrong. I have done research online to see if there is any changes, but yet none. Bellow is what i did
settings.py
STATIC_URL = '/static/'
STATIC_ROOT=[
os.path.join(BASE_DIR,"static"),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
url.py
urlpatterns +=static (settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns +=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
model.py
class product(models.Model):
imageone = models.ImageField(upload_to="productimage",null=True,blank=False)
views.py
def home(request):
product= product.objects.all()
context={'proudct':product}
template_name="name.html"
return render(request,template_name,context)
in template
{% for products in product %}
<img src="{{proudcts.imageone.url}}">
{% endfor %}
When i upload image from the admin, i get image location at http://127.0.0.1:8000/media/productimage/homepagebizalAfric.jpg, but image does not display. What i my doing wrong ?
The spelling in
context={'proudct':product}
{{proudcts.imageone.url}}
is correct?
i followed this tutorial about displaying profile pictures on website, i did everything correctly (i hope so) but instead of returning example.com/media/posters/pic1.jpg it returns example.com/pictures/pic1.jpg (real example)
settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
views.py
def index(request):
movies = {'movies' : movie.objects.all()}
return render(request, 'index.html', movies)
models.py
class movie(models.Model):
poster = models.ImageField(upload_to='posters')
and html
{% for a in movies %}
<img src="{{ a.poster }}" alt="">
{% endfor %}
i added + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) to urls.py file
also, i had to use a.poster instead of poster.image.url, it just returned unknow, different django version i assume
You need {% get_media_prefix %}.
I am trying to load an image from a folder within 'media' folder (media/tshrirts) onto template using django. Below is my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_URL = '/media/'
#MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_ROOT = 'media'
**I tried both the roots none of them work.
Below is my models.py
from django.db import models
# Create your models here.
class tshirts(models.Model):
brand=models.CharField(max_length=50)
name=models.CharField(max_length=100)
price=models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to='tshirts/')
def __str__(self):
return self.name
this is part of the tshirts.html Why image.url is none??
<div class='tshirts'>
{% for eachtshirt in alltshirts %}
<div class='tshirt'>
{{eachtshirt.brand}}
<p>{{eachtshirt.name}}</p>
<p>{{eachtshirt.price}}</p>
<img src="{{eachshirt.image.url}}"/>
{% if eachshirt.image.url == None %}
<p>{{"Image Not Found"}}</p>
{% endif %}
</div>
{% endfor %}
</div>
finally, urls.py
urlpatterns = [
.
.
.
url(r'^tshirts/',include('tshirts.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
After I uploaded the image as an admin and clicked the link, the image was properly displayed.
http://127.0.0.1:8000/media/tshirts/t-shirt2.jpg - the image was displayed here.
How can I fix this, please let me know thanks!!!
Screenshot for the page
The root of your media, should include the BASE_DIR, like the following:
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
You have a typo in your template, you'are accessing {{eachshirt.image.url}} without t. the correct is
{{eachtshirt.image.url}} <!-- with t : each tshirt -->
\project_structure
-app
\project
-settings.py
-...
\picture
-panda.jpg
I've uploaded the picture into picture.
class Goods(models.Model):
pic = models.ImageField(upload_to='picture')
And the data in database is picture/panda.jpg
Now,how can i show it in html?
I wrote this in html:
<p>{{each.pic}}</p>
<img src='{{ each.pic.url }}' />
And the source codes in browser is this:
picture/panda.jpg
<img src='picture/panda.jpg' />
The image was linked to http://localhost:8000/my_sell/picture/panda.jpg.And couldn't show.
How can i solve this,I've tried add media_root in settings.py and useless.
I did not check this but almost all code are taken from my working projects :).
settings.py:
MEDIA_URL = '/media/'
MEDIA_ROOT = (
os.path.join(BASE_DIR, 'media')
)
models:
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)
views:
from django.shortcuts import render
def view_picture(request):
c = dict()
c['goods'] = Goods.objects.all()
return render(request, 'template.html', c)
templates:
{% for product in goods %}
{% if product.pic %}
<img src="{{ product.pic.url }}">
{% endif %}
{% endfor %}
Edited: Don't forget to add MEDIA_URL into root urls.py
if settings.DEBUG
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)