0

First of all apologies for my earlier post which I accidently submitted before finishing the question... I'm not spamming :)

This seems to be a source of much confusion judging by the amount of similar titles on the subject, however after reading everything I could find on static files with the django development server i've almost given up hope!

So my static files are served from C:/Users/Dan/seminarWebsite/static/, in which i have sub folders for images, css etc. For simplicity lets just say I want an image to show on my website homepage (an easy task I would have hoped!)

SETTINGS:  
STATIC_ROOT = 'C:/Users/Dan/seminarWebsite/static/'  
STATIC_URL = '/static/'  

The static files app is also active.

URLS:  
from django.contrib.staticfiles.urls import staticfiles_urlpatterns  
urlpatterns += staticfiles_urlpatterns()

TEMPLATE: "{{ STATIC_URL }}images/vision.jpeg"

However only a broken link appears and at this address: http://127.0.0.1:8000/homepage/images/vision.jpeg; and i don't think it should be at that address (homepage is the url name of the page the staic image file is being called to)

Any help would be fantastic as its grinding my project to a hault.

Thank you

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
dannymilsom
  • 2,386
  • 1
  • 18
  • 19

3 Answers3

2

The url for your file should be http://127.0.0.1:8000/static/images/vision.jpeg

If your template has the following:

TEMPLATE: "{{ STATIC_URL }}images/vision.jpeg"

That means STATIC_URL isn't showing anything, which means you're probably not using RequestContext or you don't have the static context processor.

When debugging things like this, make sure you actually look in the template for what URL is being generated.

https://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontext

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
  • I now have that URL for the image location so I'm getting somewhere thank you! But it says that the 'images\vision.jpeg' cannot be found. I am using the RequesContext and do have static context processer linked up. My view: def homepage(request): seminars = Seminar.objects.order_by('date') return render_to_response('homepage.html', {'seminarData': seminars}, context_instance=RequestContext(request)) – dannymilsom Feb 07 '12 at 23:48
2

Check in your settings if DEBUG = False. If False - the built-in server will not serve static files unless you force it to do so with:

django-admin.py runserver --insecure

Here is a link to django docs about the The staticfiles app

--insecure

Use the --insecure option to force serving of static files with the staticfiles app even if the DEBUG setting is False. By using this you acknowledge the fact that it's grossly inefficient and probably insecure. This is only intended for local development, should never be used in production and is only available if the staticfiles app is in your project's INSTALLED_APPS setting.

Community
  • 1
  • 1
forbdn
  • 186
  • 1
  • 6
0

Finally worked! Everyones advice was very helpful thank you. For some reason I had a static subdirectory in my static directory and Django was looking in that...

Static  
-Admin  
-CSS  
-Images  
    :Vision.jpeg  
-....  
-Static  
    -Admin  
    -CSS  
    -Images  
          :Vision.jpeg  

http://127.0.0.1:8000/static/static/images/vision.jpeg

Don't really understand why but it works so thank you though

dannymilsom
  • 2,386
  • 1
  • 18
  • 19