1

我无法让我的静态文件工作。我花了 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"/>
4

3 回答 3

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()
于 2012-11-29T18:32:09.780 回答
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))
于 2012-11-29T19:18:25.757 回答
0

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

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

STATIC_URL = '/static/'

这有效:

STATIC_URL = 'http://localhost:8000/static/'
于 2014-09-08T01:07:22.793 回答