My page cound not load image, help!

16 views
Skip to first unread message

Dae James

unread,
Oct 22, 2012, 6:00:22 AM10/22/12
to django-users
The returned html code by my app is : <img src="photos/image.jpg" />
The "image.jpg" file is in "project_dir/photos/" directory. PS: project_dir is my django project's root directory.
 
So why the image could not be loaded. Please help~
Thank you~
 

Dae James

Joel Goldstick

unread,
Oct 22, 2012, 7:44:26 AM10/22/12
to django...@googlegroups.com
On Mon, Oct 22, 2012 at 6:00 AM, Dae James <daed...@126.com> wrote:
> The returned html code by my app is : <img src="photos/image.jpg" />
> The "image.jpg" file is in "project_dir/photos/" directory. PS: project_dir
> is my django project's root directory.
>
> So why the image could not be loaded. Please help~
> Thank you~
>
Because your image is not in the directory that your html file shows.
Go to the django documentation and try to understand static files.

> ________________________________
> Dae James

--
Joel Goldstick

Amyth Arora

unread,
Oct 22, 2012, 8:28:41 AM10/22/12
to django...@googlegroups.com
Serve the image as a static file. in your settings.py file set the following:

STATIC_URL = '/static/'
STATICFILES_DIRS = ('/path/to/your/image/directory',) # Without a trailing slash

and then load your image as <img src="/static/image.jpg" />



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Thanks & Regards
----------------------------

Amyth [Admin - Techstricks]
Email - aroras....@gmail.com, ad...@techstricks.com
Twitter - @mytharora
http://techstricks.com/

Dae_James

unread,
Oct 22, 2012, 9:13:59 AM10/22/12
to django...@googlegroups.com
I use ImageField(with upload_to = 'photos') to receive user-uploaded images. And in my template, I want to use <img src="{{ object.image.url }}" />(image is the object of ImageField) to display them. However, images can't be displayed. I think it's not about static files. If it is, what's the use of ImageField's url property.

在 2012年10月22日星期一UTC+8下午7时46分04秒,Joel Goldstick写道:

Xavier Ordoquy

unread,
Oct 22, 2012, 10:23:21 AM10/22/12
to django...@googlegroups.com

Le 22 oct. 2012 à 15:13, Dae_James <daed...@126.com> a écrit :

I use ImageField(with upload_to = 'photos') to receive user-uploaded images. And in my template, I want to use <img src="{{ object.image.url }}" />(image is the object of ImageField) to display them. However, images can't be displayed. I think it's not about static files. If it is, what's the use of ImageField's url property.

Moreover, images will relate to the MEDIA_*

Static are really meant to be static, ie they won't change as long as you don't update your project/applications.
Media are where other files should go. Whether they are uploaded user documents or application generated ones, they should go there.

For moreinformation have a look there:
And especially the part after: "Using a FileField or an ImageField (see below) in a model takes a few steps:"

Regards,
Xavier Ordoquy,
Linovia.
Reply all
Reply to author
Forward
0 new messages