django中的静态文件

时间:2012-11-29 18:24:19

标签: django static

我无法让静态文件生效。我花了6个小时查看关于这个主题的很多帖子,但我仍然必须做错事。请帮忙。 以下是我的设置:

projectfiles
|
|-----myproject
|     |
|     |-----static
|     |     |
|     |     |-----css
|     |     |-----js
|     |-----__init__.py
|     |-----settings.py
|     |-----urls.py
|     |-----wsgi.py
|
|-----myapp
|
|-----templates





settings.py
import os
SITE_ROOT = (os.path.realpath(os.path.dirname(__file__))).replace('\\','/')
DEBUG = True
MEDIA_ROOT = (os.path.join(SITE_ROOT, '/static')).replace('\\','/')
MEDIA_URL = '/static/'
STATIC_ROOT = ''
STATIC_URL = ''
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )





urls.py
urlpatterns = patterns('',
    (r'^myurl/$', myview),
)
from myproject.settings import DEBUG
if DEBUG:
    urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve',

                                 {'document_root': 'static'}))





mytemplate.html
...
<head>
<link src="css/mycss.css" rel="stylesheet" type="text/css"/>
...

该应用程序工作正常,但没有与我的CSS或javascripts连接。我在猜什么?

任何帮助都会非常感激。

Update:

`STATIC_ROOT='C:/path/to/myproject/static/' 
STATIC_URL='/static/' 
TEMPLATE_CONTEXT_PROCESSORS=('...static', ) 
STATICFILES_DIRS=('C:/absolute/path/to/myapp/static',) 
STATICFILES_FINDERS=('...FileSystemFinder','...AppDirectoriesFinder',) 
INSTALLED_APPS = (...,'django.contrib.staticfiles',) 

#does not work with or without this: 
urlpatterns += staticfiles_urlpatterns() 
#views now rendered like this: 
myview(request): 
... 
    return render_to_response('template.html',{'a': a},context_instance =RequestContext(request)) 


#template.html 
<link src="{{STATIC_URL}}css/mycss.css"/>

3 个答案:

答案 0 :(得分:3)

MEDIA_ROOT和MEDIA_URL不用于提供来自django 1.3的staticfiles应用程序的静态内容,因此,我建议使用STATIC_URL和STATIC_ROOT来配置静态文件。

#settings.py

STATIC_ROOT = "Absolute path to your static dir"
STATIC_URL  = "/static/"

#views:Make sure to pass RequestContext to the template.

def view_to_display_the_page(request)
    ...
    return render_to_response("templae.html", context_instance = template.RequestContext(request))

#template:

<script src="{{STATIC_URL}}path_your_static_file_relative_to_static_dir"></script>

#urls.py: Make sure to add url patterns for static file
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

答案 1 :(得分:0)

# RTFD: https://docs.djangoproject.com/en/dev/howto/static-files/

# Points to a directory for storing file uploads. Should be located outside of your package sources, i.e. your 
MEDIA_ROOT = os.path.join(SITE_ROOT, '..', 'media')
MEDIA_URL = '/media/'

# Points to the directory your static asset sources get
# collected/precompiled for distribution. Should be located outside 
# of your package sources. If STATICFILES_DIRS includes STATIC_ROOT, 
# an exception is raised.
STATIC_ROOT = os.path.join(SITE_ROOT, '..', 'static')
STATIC_URL = '/static/'

# Can be left empty if your static sources are on paths discoverable by
# STATICFILES_FINDERS. This is not true in your case, so configure the path.
STATICFILES_DIRS = [os.path.join(SITE_ROOT, 'static')]

# Use this to get the STATIC_URL path as a context variable in your templates
TEMPLATE_CONTEXT_PROCESSORS = (
    # ...
    'django.core.context_processors.static',
)

<!--And make use of it-->
<script src="{{STATIC_URL}}js/lib/jquery.js"></script>

# Run this as a test preflight to see if you've configured the settings correctly
# $ python manage.py --collectstatic
#
# Make sure you don't get any errors.

# Make sure you've hooked the static files app URLs into your main URL modules patterns
from django.conf import settings
from django.conf.urls import staticfiles_urlpatterns, static

urlpatterns = (patterns('',
    # ...
) + staticfiles_urlpatterns()
  + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT))

答案 2 :(得分:0)

使用Django 1.6版; STATIC_URL不能成为一个词干;不知道什么时候改变了。

settings.py中的以下类型的规范给出了404:

STATIC_URL = '/static/'

这有效:

STATIC_URL = 'http://localhost:8000/static/'