shanyu
shanyu

Reputation: 9716

url method of ImageField returns a non-Url value - Django

I'm developing using Django on Windows. I have a model with an imagefield, and use a form to fill it. Images get uploaded without problem. The problem occurs when I attempt to show an uploaded image inside a template by coding this:

<img src ='{{object.image.url}}'/>   

(object is an instance of the relevant model, and image is the name of the image field)

However, the image is not displayed, as the url method returns not a URL but a path like this:

c:/pp/pm/static/image/xyz.png

What should I do to make it a real URL?

edit:

MEDIA_ROOT and MEDIA_URL settings are below:

MEDIA_ROOT = 'C:/pp/pm/static/'
MEDIA_URL = '/static/'

Upvotes: 1

Views: 4250

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40502

I had the same problem. At model declaration, I changed "upload_to" argument value from absolute path to relative, this fixes the problem.

Upvotes: 2

Related Questions