0

I probably did something stupid but when i upload a image with /admin I can't get it to load.

I made a for loop to get all post's and everything shows except for the image

model:

`class Post(models.Model):
    title   =   models.CharField(max_length=140)
    body    =   models.TextField()
    image   =   models.ImageField(upload_to='blog/media/photos')
    date    =   models.DateTimeField()`

template page:

`   {% for post in object_list %}
        <h5><a href="/blog/{{post.id}}">{{post.title}}</a></h5>
        <img src="{{ post.image.url }}">
        <h1>{{ post.image.url }}</h1>
    {% endfor %}`

2 Answers2

0

I would imagine it is related to your MEDIA_URL settings. Check the terminal output and look for the request to the image, there is a good chance you've not set that, and/or you've not added the media url handler to your urls.

https://docs.djangoproject.com/en/2.0/howto/static-files/#serving-files-uploaded-by-a-user-during-development

justcompile
  • 3,362
  • 1
  • 29
  • 37
0

You must set the media folder path in the settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = '/media/'

or add media root on url.py

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Is there a blog/media/photos folder path and accessable?

aziminia
  • 449
  • 1
  • 5
  • 15