How do I serve CSS to Django in development?

后端 未结 4 1420
心在旅途
心在旅途 2021-02-02 03:37

I\'ve been all through the documentation, and it just doesn\'t make sense to me. I ran collectstatic, I set up /static/ directories in both my app and my project directories, I

相关标签:
4条回答
  • 2021-02-02 04:23

    Have a look at Serving static files in development. You need to define the STATIC_URL and STATICFILES_DIRS to let django.contrib.staticfiles know where to look for files.

    0 讨论(0)
  • 2021-02-02 04:25

    I have the same problem, and search many answers, but no one give me right answer. The problem is you don't use RequestContext I think. You should make a RequestContext as the parameter of Template like

    c = RequestContext(request, {
        'foo': 'bar',
    })
    

    In my views is:

    return render_to_response('parts/test2.html', RequestContext(request, locals()))
    
    0 讨论(0)
  • 2021-02-02 04:30

    The idea behind the static files idea is that you can distribute your development related media file (css/js etc.) on a per-app basis, and allow the static files application to manage and collect all these resources from their various places.

    So you tell the static files app where to look for static files (by settings STATICFILES_DIRS), where to copy to them (STATIC_ROOT) and what path to access them (STATIC_URL). When you run collectstatic, it search through the directories and copies all the files it finds into the static root.

    The benefit of this is that you can manage your static files on a finer leve:

    project/app1/static/css/ # These are css/js for a particular app
    project/app2/static/css/
    project/app3/static/css/
    project/static/css # These might be general css/js for the whole project
    static/ # This is where the collectstatic command will copy files to
    

    and after you collectstatic them you will have:

    project/app1/static/css/
    project/app2/static/css/
    project/app3/static/css/
    project/static/css
    
    static/app1/css/
    static/app2/css/
    static/app3/css/
    static/css/
    

    When you put your app/site on a production server, you let the webserver (apache, nginx) deal with serving the files by telling it to serve media files at /static/ or /media/ directly, while passing all other requests to the application. When developing though, it's easier to let the development server do this for you.

    To do this, you have explicitly tell is server any request for media under /static/ (your STATIC_URL). In your urls.py, put the following (or similar)

    from django.conf import settings
    ...
    if settings.DEBUG:
        urlpatterns += patterns('',
            url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),
            url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True }))
    
    0 讨论(0)
  • 2021-02-02 04:34

    Here's how mine is setup. It sounds like you might be missing the static context processor?

    STATIC_ROOT and STATIC_URL

    In the settings.py used in development:

    STATIC_ROOT = ''
    STATIC_URL = '/static/'
    

    And the settings.py used on my production server:

    STATIC_URL = '//static.MYDOMAIN.com/'
    STATIC_ROOT = '/home/USER/public_html/static.MYDOMAIN.com/'
    

    So, all the static files are located in static/. On the production server, all these files in static/ are collected to /home/USER/public_html/static.MYDOMAIN.com/ where they are served by a different web server (nginx in my case) and not Django. In other words, my django application (running on Apache) never even receives requests for static assets in production.

    CONTEXT PROCESSOR

    In order for templates to have the STATIC_URL variable available to them, you need to use the django.core.context_processors.static context processor, also defined in settings.py:

    TEMPLATE_CONTEXT_PROCESSORS = (
        # other context processors....
        'django.core.context_processors.static',
        # other context processors....
    )
    

    SERVER STATIC ASSETS IN DEVELOPMENT

    Django doesn't get requests for static assets in production, however, in development we just let Django serve our static content. We use staticfiles_urlpatterns in urls.py to tell Django to serve requests for static/*.

    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    # .... your url patterns are here ...
    urlpatterns += staticfiles_urlpatterns()
    
    0 讨论(0)
提交回复
热议问题