Images from ImageField in Django don't load in template

后端 未结 7 1409
温柔的废话
温柔的废话 2020-12-08 15:58

I\'m building a gallery using Django(1.5.1) on my local machine. In my Album model I have a ImageField. There is a view to show all images of an album. It works

7条回答
  •  情话喂你
    2020-12-08 16:32

    I have a clue on what's the problem. MEDIA_URL should be like this:

    MEDIA_ROOT='' (i.e: '/home/ike/project/media/')
    MEDIA_URL='/media/'
    

    Note the slash character at the beginning. That is because media is a folder in your root server folder and not relative to whatever other url you call it.

    And add these lines to the end of your urls.py file:

    # You might need to import static function like this:
    #from django.contrib.staticfiles.urls import static
    
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    You can check the following documentation: https://docs.djangoproject.com/en/dev/howto/static-files

    Hope this helps

提交回复
热议问题