0

Like the 1 billionth static file question about Django 1.3. I've been searching and trying so many things but nothing seems to work for me. Any help would be appreciated. Will try and give as much information as possible.

URL FILE

urlpatterns = patterns('',
url(r'^projectm/statictest/$','project_management.views.statictest'),)

VIEW

def statictest(request):
return render_to_response('statictest.html',locals())

TEMPLATE

<html><head><title>Static Load Test Page</title></head>
<body>
{% load static %}
<img src="{{ STATIC_URL }}testimage.jpg" />
</body></html>

SETTINGS

STATIC_ROOT = '/home/baz/framework/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ('',)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_DIRS = (
"/home/baz/framework/mysite/templates"

FILES

-bash-3.2$ pwd
/home/baz/framework/mysite/templates
statictest.html

-bash-3.2$ pwd
/home/baz/framework/mysite/project_management/static
-bash-3.2$ ls
testimage.jpg

Not too sure if there is any other information that would be helpful. But basically when I go to this test page and check the page source, the url is pointing to

<img src="/static/testimage.jpg" />

However the image does not load. I have tried this on multiple browsers too. Maybe I am missing an imort statement somewhere?

cheers for the help

dlyxzen
  • 243
  • 7
  • 17

2 Answers2

0

Are you using the built in runserver command or do you serve the Django app some other way?

If you use runserver then your problem is that you don't tell Django where to find your static assets in the filesystem. You need to set STATIC_ROOT to a filesystem path where your static assets can be found. Try setting it to: /home/baz/framework/mysite/project_management/static/

If you are using a different server (like gunicorn behind Nginx for example) then it is the responsibility of the front-end server to intercept requests for /static/ and serve them for you.

Daniel Eriksson
  • 3,814
  • 3
  • 16
  • 11
  • STATIC_ROOT is where runserver looks for files to serve (only works when DEBUG=True). It is also where Django copies static files during collectstatic. The collectstatic command, by default, looks for static files in /static/ in each app plus in any dirs listed in STATICFILES_DIRS. – Daniel Eriksson Feb 18 '13 at 02:03
  • Yes I am using the built it runserver. – dlyxzen Feb 18 '13 at 03:25
0

Also remember to run the ‘collectstatic’ management command once you have set ‘STATIC_ROOT’.

https://docs.djangoproject.com/en/1.4/howto/static-files/#deploying-static-files-in-a-nutshell

Matt
  • 8,758
  • 4
  • 35
  • 64
  • Hi, okay so I updated the STATIC_ROOT setting in my config file. Performed a python manage.py collectstatic. I can see it has collected the testimage from my apps static folder, but when I refresh the page the image still doesn't load, and the path is simply imgsrc='testimage.jpg' . – dlyxzen Feb 18 '13 at 03:24
  • Since you're testing this with the built-in runserver you'll need to tweak your urls.py a little bit, [see here](https://docs.djangoproject.com/en/1.4/howto/static-files/#serving-static-files-in-development). Now once that's done and your server is running, try to access the image directly by going to `http://your-url/static/testimage.jpg` – if that works ok but the template is still outputting "src='testimage.jpg'" instead of "src='/static/testimage.jpg'" then you're not using RequestContext when the template is rendered, [see here](http://stackoverflow.com/a/11684482/1428653). – Matt Feb 18 '13 at 05:40
  • I was thinking it could have been something to do with permissions so I tried chmodding the the static directories to 777 but to no avail. – dlyxzen Feb 18 '13 at 05:40
  • try `{% get_static_prefix %}`. ref: http://stackoverflow.com/questions/5457737/access-static-url-from-within-a-custom-inclusion-template-tag/5457817#5457817 – yuwang Feb 18 '13 at 06:02
  • Hey Matt,I changed the imgsrc to "/static/testimage.jpg" and it seemed to work. It pulled it out of the project app folder. But when I use the static tag it breaks again. I'm sure I am doing something wrong in the settings or something. thanks – dlyxzen Feb 18 '13 at 06:03
  • I changed it to the get_static_prefix tag instead of the STATIC tag and it worked! I might have changed some things along the way to make it work that way, not too sure. Appreciate the help guys, truly. :) – dlyxzen Feb 18 '13 at 06:06