How do I display a static image in my index.html file?

136 views
Skip to first unread message

frocco

unread,
Jan 11, 2013, 3:20:40 PM1/11/13
to django...@googlegroups.com
Hello,

I am a newbie and cannot figure out how to do a simple image display that I currently do in PHP.
I want to display a logo on my home page.

I tried google, but am having no luck.

Thanks

@jeffblack360

unread,
Jan 11, 2013, 3:35:58 PM1/11/13
to django...@googlegroups.com

frocco

unread,
Jan 11, 2013, 11:12:57 PM1/11/13
to django...@googlegroups.com
I am still stuck.

Nikolas Stevenson-Molnar

unread,
Jan 12, 2013, 12:38:03 AM1/12/13
to django...@googlegroups.com
Could you provide a bit more detail on the problem you're having?

_Nik
--
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/-/8g4-QNDvZdAJ.
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.

frocco

unread,
Jan 12, 2013, 9:10:10 AM1/12/13
to django...@googlegroups.com
[code]
#staff/views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from staff.models import Staff

def index(request):
    return render_to_response('staff/index.html')
*****
Index.html in folder templates/staff

<img src="auto.jpg" />

*****

auto.jpg in folder img


My site is:

store
  img
    auto.jpg
  staff
     views.py
  templates
     staff
        index.html

image is not showing.
This needs to work in development and production

On Friday, January 11, 2013 3:20:40 PM UTC-5, frocco wrote:

Jeffrey Black

unread,
Jan 12, 2013, 12:24:34 PM1/12/13
to django...@googlegroups.com
frocco:

First things first: What version of Django are you running, and what OS?

Thanks,


Jeff
@jeffblack360

Jeffrey Black

unread,
Jan 12, 2013, 12:58:08 PM1/12/13
to django...@googlegroups.com
Double-check your the static settings in settings.py and try this:

1: Make sure static URL is '/static/'

2: <img src="/static/img/auto.jpg" />

3: Move your img directory to store/static directory.

4: Try and hit your image via browser with http://localhost:8000/static/img/auto.jpg

5: Read the Django documentation about managing static files:
https://docs.djangoproject.com/en/1.4/howto/static-files/

The django.contrib.staticfiles module will pick up static directories in your app directory.

Good luck.

@jeffblack360
--
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/-/_yjULDxZe2YJ.

frocco

unread,
Jan 12, 2013, 2:45:48 PM1/12/13
to django...@googlegroups.com
Hello,

django 1.4 on mac.

plan to deploy to Bluehost.com when finished.

I have a php site I want to convert to django.

but honestly, django is harder for me to learn than php, but I am determined.
Message has been deleted
Message has been deleted
Message has been deleted

frocco

unread,
Jan 12, 2013, 3:23:34 PM1/12/13
to django...@googlegroups.com
Google keeps deleting my reply.

I tried the url to the static/img path and get 404 error.

Nikolas Stevenson-Molnar

unread,
Jan 12, 2013, 5:17:10 PM1/12/13
to django...@googlegroups.com
Add your static files directory to STATICFILES_DIRS. Make sure to include a trailing comma. E.g:

STATICFILES_DIRS = (
    os.path.join(DIR,'static'),
)

_Nik

On 1/12/2013 12:09 PM, frocco wrote:
Page not found (404)
Request Method: GET
'img/auto.jpg' could not be found

[code]
# Django settings for store project.
import os
import django.contrib.staticfiles

DIR = os.path.abspath(os.path.dirname(__name__))

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_...@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'store',                      # Or path to database file if using sqlite3.
        'USER': 'root',                      # Not used with sqlite3.
        'PASSWORD': 'root',                  # Not used with sqlite3.
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/New_York'

# Language code for this installation. All choices can be found here:
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(DIR,'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
MEDIA_URL = ''

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = os.path.join(DIR,'static')

# URL prefix for static files.
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.
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'ne#cnv$g0^b6fl5$u#zue=5jw8898@c5jqrrh694jzrr-ob5!n'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'store.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'store.wsgi.application'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(DIR,'templates'),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'staff',
    'page',
    'tinymce',
    'django.contrib.staticfiles',
)

# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# more details on how to customize your logging configuration.
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    )
[/code]
--
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/-/yBn4RncmKRYJ.

Jeffrey Black

unread,
Jan 12, 2013, 5:41:12 PM1/12/13
to django...@googlegroups.com, django...@googlegroups.com
Directory structure please?

FWIW, you have django.contrib.staticfiles defined twice.

Did you read the howto doc on static files?

@jeffblack360

On Jan 12, 2013, at 2:17 PM, frocco <far...@gmail.com> wrote:


Page not found (404)

Request Method:GET
Request URL:http://127.0.0.1:8000/static/img/auto.jpg

Using the URLconf defined in ntw.urls, Django tried these URL patterns, in this order:

  1. ^staff/$
  2. ^static/img/$
  3. ^static/$
  4. ^admin/doc/
  5. ^admin/

The current URL, static/img/auto.jpg, didn't match any of these.

--
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/-/3Qt8rkRKSrcJ.
Message has been deleted
Message has been deleted

Mike Dewhirst

unread,
Jan 13, 2013, 9:12:12 PM1/13/13
to django...@googlegroups.com
On 14/01/2013 2:18am, frocco wrote:
> I finally got this working.
>
> Why is it necessary to run collectstatic?
> in PHP, I would just point to the images.

Consider that you might have a number of apps in your project. As an
experienced Django dev you would probably keep your separate apps nicely
segregated so they can be re-used in multiple projects without any
integration overhead. That means the static files for such apps really
must stay with the apps - or management and deployment gets too hard.

So your project has a few apps and each has its own static files. When
you deploy all you need to do is add the app static dir to the
settings.STATICFILES_DIRS list and run collectstatic.

Apache or whatever webserver you use only needs a simple directive to
serve them from STATIC_ROOT.

It is very nice.

>
> Thanks for your help, I am really enjoying django.
>
> On Saturday, January 12, 2013 5:41:12 PM UTC-5, @jeffblack360 wrote:
>
> Directory structure please?
>
> FWIW, you have django.contrib.staticfiles defined twice.
>
> Did you read the howto doc on static files?
>
> @jeffblack360
>
> On Jan 12, 2013, at 2:17 PM, frocco <far...@gmail.com <javascript:>>
> wrote:
>
>>>
>> Page not found (404)
>>
>> Request Method: GET
>> Request URL: http://127.0.0.1:8000/static/img/auto.jpg
>> <http://127.0.0.1:8000/static/img/auto.jpg>
>>
>> Using the URLconf defined in |ntw.urls|, Django tried these
>> URL patterns, in this order:
>>
>> 1. ^staff/$
>> 2. ^static/img/$
>> 3. ^static/$
>> 4. ^admin/doc/
>> 5. ^admin/
>>
>> The current URL, |static/img/auto.jpg|, didn't match any of these.
>>
>> --
>> 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/-/3Qt8rkRKSrcJ
>> <https://groups.google.com/d/msg/django-users/-/3Qt8rkRKSrcJ>.
>> To post to this group, send email to django...@googlegroups.com
>> <javascript:>.
>> To unsubscribe from this group, send email to
>> django-users...@googlegroups.com <javascript:>.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en
>> <http://groups.google.com/group/django-users?hl=en>.
>
> --
> 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/-/3UmqFDipFRQJ.

frocco

unread,
Jan 13, 2013, 10:33:14 PM1/13/13
to django...@googlegroups.com
I see...

Thanks for the explanation.

--Frank


On Friday, January 11, 2013 3:20:40 PM UTC-5, frocco wrote:
Reply all
Reply to author
Forward
0 new messages