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 または JavaScript に接続できません。私は何が欠けていますか?

どんな助けでも大歓迎です。

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 に答える