开发者

Django 1.3 static files and displaying content

开发者 https://www.devze.com 2023-02-24 09:27 出处:网络
I\'m new to django, I\'m trying to load a js file and image, the system of views and templates. But I have problems. Developing m开发者_Python百科y configuration is as follows.

I'm new to django, I'm trying to load a js file and image, the system of views and templates. But I have problems. Developing m开发者_Python百科y configuration is as follows.

IN SETTINGS.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    'E:/GIS/recursos/js',
)

STATIC_ROOT = ''

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',

)

URLS.py

urlpatterns = patterns('',

    url(r'^prueba/', current_datetime),
)

urlpatterns += staticfiles_urlpatterns()

The page loads but not the static files in the logs I see

[14/Apr/2011 10:55:41] "GET /prueba/ HTTP/1.1" 200 631
[14/Apr/2011 10:55:41] "GET /prueba/prueba.js HTTP/1.1" 200 631
[14/Apr/2011 10:55:41] "GET /prueba/img.jpg HTTP/1.1" 200 631
[14/Apr/2011 10:55:41] "GET /prueba/img.jpg HTTP/1.1" 200 631

thanks in advance


your static files should be served at /static/ not /prueba/ as your logs indicate... check your template.

edit:

some things to check:

  1. check your settings.py file and see that

        'django.core.context_processors.static',
    

    is in your TEMPLATE_CONTEXT_PROCESSORS

  2. check to see if DEBUG=True

  3. check that you have 'django.contrib.staticfiles' in your installed apps.

  4. Read the django docs for managing static files and context processors

    As a brief refresher, context processors add variables into the contexts of every template. However, context processors require that you use RequestContext when rendering templates. This happens automatically if you're using a generic view, but in views written by hand you'll need to explicitally use RequestContext To see how that works, and to read more details, check out Subclassing Context: RequestContext.


I was having a hard time with this new static file app, but I got it working now. Your settings.py seems pretty ok to me. Apparently the trick is to use the proper context_instance at your view. There is no need to use staticfiles_urlpatterns() in development mode.

So this is what I added in my view:

from django.template import RequestContext

[...]
def my_view(request):
   [...] 
   return render_to_response("templatename.html", {'form': form},
context_instance=RequestContext(request))

Or you could also use the render shortcut, which works like the render_to_response with a RequestContext:

from django.shortcuts import render

[...]
def my_view(request):
   [...] 
   return render(request, 'templatename.html', {'form': form})

HTH

reference: http://groups.google.com/group/pyweb-il/browse_thread/thread/957d6215e3716fa7?pli=1

0

精彩评论

暂无评论...
验证码 换一张
取 消