static files in django

2019-06-06 17:04发布

问题:

I am unable to get my static files to work. I've spent 6 hours looking at the many posts on this topic but I must still be doing something wrong. Please help. Here are my settings:

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"/>
...

the app works fine but no connection to my css's or javascripts. What am I misssing?

Any help would be sooo greatly appreciated.

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"/>

回答1:

MEDIA_ROOT AND MEDIA_URL are not used for serving static content with advent of the staticfiles app from django 1.3 so, I suggest using STATIC_URL and STATIC_ROOT instead to configure the static files.

#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()


回答2:

# 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))


回答3:

Using Django version 1.6; STATIC_URL cannot be a stem; Not sure when this changed.

The following type of specification in settings.py gives a 404:

STATIC_URL = '/static/'

This works:

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


标签: django static