2

In Django 1.8, I can upload images and they are saved on the path that they should, but when I add the path to template, the images are not displaying. I GET 200 http code for them in terminal server.

Here are the relevant parts in settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

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

MEDIA_URL = '/media/'

And in urls.py I have added:

urlpatterns = [
...

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

Example image tag path:

<img src="/media/photos/1/1430598029_08_ranaplazaslide.jpg" />

and the Get code:

GET /media/photos/1/1430598029_08_ranaplazaslide.jpg HTTP/1.1" 200 20888

I am wondering what is wrong with my settings and how to fix this?

Jand
  • 2,527
  • 12
  • 36
  • 66
  • 1
    Nothing wrong with your settings. 200 is an OK http code so your server doing it right. Open the `/media/photos/1/1430598029_08_ranaplazaslide.jpg` with you browser and image will be shown. – catavaran May 03 '15 at 04:01
  • What happens when you open the image url on the browser? – DigitalDouble May 03 '15 at 04:02
  • @DigitalDouble when I open the image path, the default page appears, instead of the image. – Jand May 03 '15 at 04:06
  • Update your question with the full code of your `urls.py`. Seems like you have invalid regex for home page. – catavaran May 03 '15 at 04:07
  • @catavaran you are right. When I comment out the last line in urls.py, which is `url(r'^', 'article.views.main'),` the image appears. Please answer and I'll accpet. – Jand May 03 '15 at 04:10

1 Answers1

2

Add the $ sign to the regex for your home page:

url(r'^$', 'article.views.main')
catavaran
  • 44,703
  • 8
  • 98
  • 85