show an image in django

527 views
Skip to first unread message

jianhui chen

unread,
Jan 8, 2013, 9:41:55 AM1/8/13
to django...@googlegroups.com
Hi all,
I want to show an image in project in which the folder DIR looks like this:
images/a.jpg
templates/introduction.html

In introduction.html I use
<img src="../images/a.jpg" alt = "example" width = "390" height = "225">

It can show image correctly when I open the "introduction.html" directly using firefox, but it shows ""GET /images/gauge_example.jpg HTTP/1.1" 404 " when I open it in http://127.0.0.1:8000/introduction/, and there is no image in the webpage.

Could anyone give me some suggestions?

jianhui





Mario Gudelj

unread,
Jan 8, 2013, 4:00:32 PM1/8/13
to django...@googlegroups.com

Try removing .. from the path. Also make sure your static path is set correctly in settings.py

--
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.

jianhui chen

unread,
Jan 9, 2013, 6:27:11 PM1/9/13
to django...@googlegroups.com
My setting.py is:
# Initialize App Engine and import the default settings (DB backend, etc.).
# If you want to use a different backend you have to remove all occurences
# of "djangoappengine" from this file.
from djangoappengine.settings_base import *

import os

# Activate django-dbindexer for the default database
DATABASES['native'] = DATABASES['default']
DATABASES['default'] = {'ENGINE': 'dbindexer', 'TARGET': 'native'}
AUTOLOAD_SITECONF = 'indexes'

SECRET_KEY = '=r-$b*8hglm+858&9t043hlm6-&6-3d3vfc4((7yd0dbrakhvi'

INSTALLED_APPS = (
#    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.staticfiles',
    'djangotoolbox',
    'autoload',
    'dbindexer',
    'shape_practice',

    # djangoappengine should come last, so it can override a few manage.py commands
    'djangoappengine',
   
)

MIDDLEWARE_CLASSES = (
    # This loads the index definitions, so it has to come first
    'autoload.middleware.AutoloadMiddleware',

    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
)

# This test runner captures stdout and associates tracebacks with their
# corresponding output. Helps a lot with print-debugging.
TEST_RUNNER = 'djangotoolbox.test.CapturingTestSuiteRunner'

ADMIN_MEDIA_PREFIX = '/media/admin/'
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)

ROOT_URLCONF = 'urls'
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.   
    "E:/code/python/djangoBook/django-testapp-develop/static",  #this only used for local debug
)

and I use STATIC_URL in the html
<img src="{{ STATIC_URL }}images/gauge_example.jpg" alt = "aa gauge example" width = "390" height = "225" />
But, it must be something wrong in my setting.py since the project can't find the image.
Any suggestions? Thanks.

Mario Gudelj

unread,
Jan 10, 2013, 4:46:18 PM1/10/13
to django...@googlegroups.com
Have you tried removing {{ STATIC_URL }} and replacing it with a slash (/)? Or try appending the slash to the end of "E:/code/python/djangoBook/django-testapp-develop/static" in STATICFILES_DIRS.

You can also add DJANGO_ROOT = dirname(dirname(abspath(__file__))) to the top of your settings.py and then add this below it:


STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    normpath(join(DJANGO_ROOT, 'static')),
)


That works for me in every project, although I don't use Windows, so it may be different in your case.
Message has been deleted

jianhui chen

unread,
Jan 12, 2013, 8:12:43 PM1/12/13
to django...@googlegroups.com
Really thanks your help. Finally, I find the problem is not in django settings, but in google engine settings.
By trying many methods, my problem problem is solved by this way.
add
- url: /static
  static_dir: static  #my static folder is in the root of the project
  expiration: '0'
to app.yaml.

and I run command :
./manage.py collectstatic 
before I update file to google app engine, but I don't whether this command is necessary for the static issue.



On Thu, Jan 10, 2013 at 5:39 PM, @jeffblack360 <jeffre...@yahoo.com> wrote:
I think your configuration should allow you to hit your image from your browser via the following link:

http://localhost:8000/static/images/gauge_example.jpg

If that works change your replace {{STATIC_URL}} with '/static/'...

<img src="/static/images/gauge_example.jpg" alt = "aa gauge example" width = "390" height = "225" />
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/AO-VKasUjWcJ.
Reply all
Reply to author
Forward
0 new messages