Question

Helo all,

I am running a Django application in development mode. I have collected static files into a /static/images/ directory in my project.

In my template I try to link an example image:

<img src="{{ STATIC_URL }}items/no_image.jpeg"/>
    {{ STATIC_URL }}items/no_image.jpeg

(The bottom line is printing for debug purposes)

The picture shows with a broken link, and the bottom line prints out the correct directory:

 /static/items/no_image.jpeg

Inside my project, I do have the /static/items/no_image.jpeg file.

In my settings.py I have:

STATIC_ROOT = os.path.dirname(__file__)+'/static/'
STATIC_URL = '/static/'

Can anybody help?

Thank you !

Was it helpful?

Solution 2

Found out the problem. The problem was I was using the wrong directory. STATIC_URL was named /site_media/ when it should be /static/. Changed it and everything now works clear as water...

OTHER TIPS

I think it's slash issue. Try

STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')

Maybe you've misunderstood the collecting of static files. There's no need to collect them into a target directory with the collectstatic command during development. That's meant to be done for deployment.

During dev, you only add the django.contrib.staticfiles app, then specify the STATIC_ROOT, STATIC_URL and STATICFILES_DIRS as described here.

Then, in your base urlconf you need to add the url rules for the staticfiles as described here. That's it for "development mode".

In production mode, you run the collectstatic command first, then shove the resulting directory over to the server where your apache (or nginx or whatever) is running, and let him serve that directory unter www.yoururl.com/static/

The whole story about handling staticfiles in django almost drove me nuts ... and even today I have to think for some minutes when trying to remember it or explain it. :-/ Don't worry if it confuses you sometimes.

I think I recently had a similar issue. Try placing your images directly in /static/ instead of /static/items/. If you want to be able to directly link to /static/items/sample.jpg you need to add /static/items/ to your STATIC_ROOT in SETTINGS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top