0

Pls tell me. Why ../static/image.jpg show images, and ../media/image.jpg now show

Need write url? Need change Setting? i dont fiend answer in documentation.

pls help.

2 night search answer.

Need upload from admin-panel photo and show in templates.

<img src="{{ tovar.product_img.url }}">
Dimon Kudimon
  • 53
  • 3
  • 7

4 Answers4

2

To display image you need load the static files in your template before referencing them. This is how you should display a picture in your template:

{% load staticfiles %}

<img src="{% static 'image.jpg' %}">

This image needs to be stored in your static folder. ../YourApp/YourApp/static/image.jpg is where I keep my static folder. Obviously it would be better to structure it further with images folder inside the static folder etc..

In your settings file you need the following lines:

# Static asset configuration
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

This should do the trick for you.

Mazvél
  • 951
  • 1
  • 8
  • 22
  • Then why don't you move your media folder inside the static files folder or create a similar variable to reference your media folder? – Mazvél Mar 23 '15 at 20:13
0

In settings.py

MEDIA_ROOT = '/path/to/yourmediafolder/'
MEDIA_URL = '/media/' # whatever but it should same in `urls.py`

In urls.py

urlpatterns += patterns('',
    (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.MEDIA_ROOT}))

Then in template

<img src="{{ MEDIA_URL }}images/imagename.jpg"/>

Note: Here image should be as '/path/to/yourmediafolder/images/imagename.jpg'

Complete example:

I have an image test.jpg as '/home/me/test.jpg

MEDIA_ROOT = '/home/' # or /home/me/ but change url in image src
MEDIA_URL = '/media/'

#urls.py same as above

In template

<img src="{{ MEDIA_URL }}me/test.jpg"/> # or <img src="{{ MEDIA_URL }}test.jpg"/> as or condition above in MEDIA_ROOT.

Note that {{ MEDIA_URL }}me , no / between them because MEDIA_URL='/media/

You can test by:

http://domain.com/media/me/test.jpg # or  http://domain.com/media/test.jpg as OR condition in MEDIA_ROOT

in local:

http://localhost:8000/media/me/test.jpg #in locally
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
  • if i used localhost. How link in MEDIA_ROOT need use? – Dimon Kudimon Mar 23 '15 at 20:18
  • no matter what `env`. put abs path something like `/var/myimages/` – itzMEonTV Mar 23 '15 at 20:20
  • If my MEDIA_URL is '/media/', and abs path to 'media' folder is 'D:/project_django/project1/media', will MEDIA_ROOT = 'D:/project_django/project1/media' ? And if my upload_to = 'img' (in models), will my uploaded files be pushed to folder 'media/img'?? And then could I write in template – Dimon Kudimon Mar 23 '15 at 20:39
  • And what exactly imagename.jpg is? Is it an abstract name for any .jpg picture or is it a concrete name of my picture? And what if I load another picture to ImageField of another object of my Class with ImageField? How does Django uderstand which picture from 'media/img' to display in template? Wouldn't I change my code every time I load a new picture? – Dimon Kudimon Mar 23 '15 at 20:40
  • I explained an example how url and root to be set.Then one thing you have to note that 'only image name and path is saved in database`(others also).So you have to query all imagenames from models and can apply this inside a for loop. – itzMEonTV Mar 23 '15 at 20:46
  • Oh, I cant understand.. If my folder 'images' with test.jpg (just as your folder 'me' with test.jpg) is in folder 'project1' which is env, what my MEDIA_ROOT must be equal to? – Dimon Kudimon Mar 23 '15 at 21:05
  • Then `MEDIA_ROOT='/path/to/images/'`, `MEDIA_URL='/media/'` , `src={{MEDIA_URL}}test.jpg` – itzMEonTV Mar 23 '15 at 22:05
0

This works (in the html img tag): src="(left squiggly, percent) static poc.image.name (percent, right squiggly):

...and thanks to whoever pointed out that ImageField has a name in addition to a (useless) path and (useless) url e.g.:

>>> from nutr.models import *
>>> poc=POC.objects.get(pk=87)
>>> poc.name

'Edgar'

0

In Django==2.0.2 use {% get_media_prefix %}

From the docs

chidimo
  • 2,684
  • 3
  • 32
  • 47