Can't display image from media subdirectory - django

If I load image like this:
<img src="{{ MEDIA_URL }}sub/test.jpg" />
it does not work, but if the image is not in a subdirectory it work fine...
<img src="{{ MEDIA_URL }}test.jpg" />
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR + '/myApp/media'
urls.py
(r'^media/(?P<path>.*)$',
'django.views.static.serve',
{
'document_root': settings.MEDIA_ROOT,
'show_indexes': True
}),
I checked file/folder permissions and everything is fine.
Django version is 1.7.1

Related

DJango read static file Json

I am learning django, I would like to read a json file but I do not kown where to put it cause I cannot display.
I have place the file in the folder "static_project" but it is not working
Here is the structure of my project:
src=>templates=> Main=> home.html
src=>Myproject=> settings.py
src=>Myproject=>urls.py
here the html:
{% load static %}
<lottie-player
src= src="{% static "./myfile.json" %}" background="transparent" speed="1" style="width: 700px; height: 700px;" loop autoplay >
</lottie-player>
</div>
Hereis the main urls.py
if settings.DEBUG:
import debug_toolbar
urlpatterns += [
path('__debug__/', include(debug_toolbar.urls)),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Here is my setting :
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static_project")
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")
#every time a user upload folder, it will be inside the static_cdn folder and the root "media root"
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")
Thanks for your help

Images not being served in django

My media which i have uploaded using the form in this question View does not seem to be saving all the information in my model is being uploaded to the correct /media/images folder that i have defined. However, on access it returns a 404.
i have tried to follow the instructions on https://stackoverflow.com/a/39359264 but still am getting a 404.
my template code
{% for books in object_list %}
<img src="{{ books.cover_pic.url }}">
{% endfor %}
settings.py
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
my urls.py
urlpatterns =[
path('', views.index, name='index'),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
my model code for the image is
cover_pic = models.FileField(null=True, blank=True, upload_to='images/')
my media folder is at base dir (next to manage.py).
What am i overlooking?
Try this. It works for me this way. You also try some method that I commented in the models.py incase
of anything.
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_ROOT = (
BASE_DIR
)
MEDIA_URL = '/media/'
models.py ...
#Here you can try changing FileField to ImageField if you like.
# You can also try directory uploads as image without backslash
#cover_pic = models.ImageField(upload_to='images')
cover_pic = models.FileField(upload_to='images/')
urls.py(project's)
if settings.DEBUG:
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
template (.html)
{% for books in object_list %}
<img src="{{ books.cover_pic.url }}">
{% endfor %}

In Django project Media folder is not getting created

There is no media folder in the project, and i can see the media in db, but it won't load on template.
With this error:
Not Found: /DSC_0008.JPG
settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include('UserProfile.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
index.html looks something like this:
<div class="profile-userpic">
<img src="{{ my_details.associate_image.url }}" width="150" height="150" class="img-responsive" alt="my pic">
</div>
For development server,
urlpatterns = [
# ...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This will create your media folder when DEBUG = True in your settings.
Make sure your image path {{ my_details.associate_image.url }} is correct. The media url must be like 127.0.0.1:8000/media/..filename.ext..

images from media file wont get displayed in django template

I'm new to django and I've been playing around with uploading pictures then displaying them. ... well trying to display them.
Whenever I try to display the image from a template, I just get the broken image link icon.
I'm using the sqlite3 server
settings.py
ROOT_DIR = os.path.dirname(os.path.dirname(__file__))
def location(f):
return os.path.join(ROOT_DIR, f)
MEDIA_URL = 'http://127.0.0.1:8000/media/'
MEDIA_ROOT = location('media/')
models.py
class Image(models.Model):
image = models.ImageField(upload_to = 'images/')
views.py
from imageupload.settings import MEDIA_ROOT, MEDIA_URL
def main(request):
imgs = Image.objects.all()
return render_to_response('index.html', {'images': imgs, 'media_root': MEDIA_ROOT, 'media_url': MEDIA_URL})
url.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Right now I've just used the admin to upload images. And that seems to work fine, they go to where I expect
But when I try to display them:
template
<!DOCTYPE html>
<html>
<img src="<correct path to project>/media/images/photo_1.JPG" />
{% for img in images %}
<img src="{{ media_root }}{{ img.image.name }}" />
<img src="{{ media_url }}{{ img.image.name }}" />
<img src="{{ img.image.url }}" />
{% endfor %}
</html>
I get the broken icon for each one.
The browser source code shows me:
<!DOCTYPE html>
<html>
<img src="<correct path to project>/media/images/photo_1.JPG" />
<img src="<correct path to project>/media/images/photo_1.JPG" />
<img src="http://127.0.0.1:8000/media/images/photo_1.JPG" />
<img src="http://127.0.0.1:8000/media/images/photo_1.JPG" />
</html>
that makes sense, I only have one photo uploaded.
and if I copy one of the hard links and put it into some other html file ...it works
Oh FFS....
aperently it's
MEDIA_URL = '/media/'
NOT
MEDIA_URL = 'http://127.0.0.1:8000/media/'
...despite #'http://127.0.0.1:8000/media/' being written right next to it
working img link looks like so:
<img src="/media/images/photo_1.JPG" />
you definitely need the:
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in the url file also
For me, I did the following two things:
Define media url in the settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and then in the project's urls.py, defined serving url for development and production purpose:
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('django_app.urls')),
]
# Serving the media files in development mode
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
else:
urlpatterns += staticfiles_urlpatterns()
then you can reference the image in the media folder in the template like this:
<img src="media/path_to_image" />
Note: Don't forget to set the DEBUG flag to False in the settings.py for production.
if you are looking for display images in development environment try this:
settings.py
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(SITE_ROOT, 'static')
MEDIA_URL = '/static/'
STATIC_ROOT = os.path.join(SITE_ROOT, 'statics')
STATIC_URL = '/statics/'
urls.py
# somebody import this from settings
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
urlpatterns += patterns('',
url(r'^static/(?P<path>.*)$','django.views.static.serve',
{'document_root': os.path.join(SITE_ROOT, 'static')})
)
html view file *.html:
<img src="{{ MEDIA_URL }}img/content_top_edit_form.png">
this means we have a img folder in static folder. in your case you can change this by
if you see your browser html must be seen like this:
<img src="/static/img/content_top_edit_form.png">
in the template use {{img.image.name.url}}

show picture in django

I have a new django project project1.It contains basic files of django(settings.py,urls.py,views.py).then I put imgs in project1/static/img/,such as 11.jpeg
now I want to show this picutre in a url request(like http://host:port/path/to/img);or something like a response that return a template which contains <img src='...'/>
I tried many ways and nothing works
does anyone can give me some suggest,or some simple code
thanks advance!
What version of django are you using?
in 1.4 you can do this in your template:
<img src="{{ STATIC_URL }}hi.jpg" />
or
<img src="../static/hi.jpg" />
and in your settings.py:
MEDIA_ROOT = '/Users/YOURUSERNAME/Desktop/YOURPROJECTNAME/static'
^^^ this just has to be the path to the static directory in your project ^^^
MEDIA_URL = '/static/'
STATIC_ROOT = ''
STATIC_URL = '/media/'
then in your urls.py:
if settings.DEBUG: urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), )
This is what u need i guess:
<img src="{{ STATIC_URL }}img/11.jpeg" />