Images from the media will not be displayed in the django template

I am new to django and I played with downloading photos and then displaying them .... well, trying to display them.

Whenever I try to display an image from a template, I just get an icon with a broken image.

I am using 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) 

Now I just used admin to upload images. And that seems to work fine, they go 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 a broken icon for everyone.
The source code of the browser 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> 

which makes sense, I only have one photo uploaded.
and if I copy one of the hard links and put it in some other html file ... it works

+6
source share
2 answers

Oh FFS ....

and this

 MEDIA_URL = '/media/' 

NOT

 MEDIA_URL = 'http://127.0.0.1:8000/media/' 

... despite the fact that #'http://127.0.0.1:8000/media/' is written next to it

img working link looks like this:

 <img src="/media/images/photo_1.JPG" /> 

you will definitely need:

 static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

in url file also

+11
source

If you are looking for display images in a development environment, try the following:

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 * .html file:

 <img src="{{ MEDIA_URL }}img/content_top_edit_form.png"> 

this means that we have an img folder in a static folder. in your case you can change this to

if you see that your html browser should look like this:

 <img src="/static/img/content_top_edit_form.png"> 
0
source

Source: https://habr.com/ru/post/916800/


All Articles