3

I have problem with adding images in Django project, my project is

my_app
  manage.py
  my_app 
    urls.py 
    views.py 
    settings.py 
    templates
     home.html
    static
     img
       image1.png

in settings.py

is all set by default

STATIC_URL = '/static/'

In home.html I aded code

{% load staticfiles %}

<img src="{% static 'img/image1.png' %}" />

but it can't load images , page not found

127.0.0.1:1000/static/img/openicon2.png

Page not found (404)
Request Method:     GET
Request URL:    127.0.0.1:1000/static/img/openicon2.png

Can someone help me ? maybe i need to modify urls.py ?

Serjik
  • 10,543
  • 8
  • 61
  • 70
user2746078
  • 107
  • 4
  • 11
  • This has been asked atleast 200 times, please inquire the search at the top right of this page. Have you read the [documentation](https://docs.djangoproject.com/en/1.2/howto/static-files/#limiting-use-to-debug-true) which practically holds your answer? possible duplicate of [Confusion in Django admin, static and media files](http://stackoverflow.com/questions/10460028/confusion-in-django-admin-static-and-media-files) and many others – Hedde van der Heide Sep 04 '13 at 10:23
  • I read documentation, and followed steps from https://docs.djangoproject.com/en/1.5/howto/static-files/ , also I check search at the top right this page, nut nothing helps ??? – user2746078 Sep 04 '13 at 10:27
  • The documentation you refer to is for _production_ applications, where _the server_ serves static content. You are obviously in local development (DEBUG mode) where Django serves these files. Have a look at the documentation I linked and there's your answer. – Hedde van der Heide Sep 04 '13 at 10:32
  • You are requesting `openicon2.png`, and your directory only has `image1.png`, perhaps this is the problem. – Burhan Khalid Sep 04 '13 at 12:04
  • I tried https://docs.djangoproject.com/en/1.2/howto/static-files/#limiting-use-to-debug-true , and get error Page not found (404) Request Method: GET Request URL: http://127.0.0.1:1000/myapp/site_media/image.png "/path/to/media\image.png" does not exist – user2746078 Sep 04 '13 at 14:48
  • in may ap I added path/to/media/image.png – user2746078 Sep 04 '13 at 14:49

2 Answers2

1

In my experience I've done the following in my settings.py and everythings works ok

import os.path,sys
CURRENT_DIR = os.path.dirname(__file__).replace('\\','/')
PROJECT_ROOT = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir))

# other codes and stuff

I've added a variable named deveop and I change it depends on if my application runs on development server or apache

DEVELOP = True

# other codes and stuff

if DEVELOP == False:
    MEDIA_ROOT = os.path.join(PROJECT_ROOT,'media')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

# 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: "/var/www/example.com/static/"
if DEVELOP == False:
    STATIC_ROOT = os.path.join(PROJECT_ROOT,'static')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT,'static'),
    # 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.
)
Serjik
  • 10,543
  • 8
  • 61
  • 70
  • Don't create a new setting `DEVELOP` when there is already `DEBUG`. Your `STATICFILES_DIRS` should not include `STATIC_ROOT`. – Burhan Khalid Sep 04 '13 at 12:03
1

Simply add,

In settings.py:

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

In project's urls.py :

from django.conf.urls.static import static
from my_app import settings

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

Then you can add image dynamically!!

In Templets:

<img ... src="{{key.image.url}}" ... >
dipak
  • 107
  • 1
  • 1
  • 6