How to refer to static files in my css files?

后端 未结 8 1744
醉话见心
醉话见心 2020-12-24 11:19

I have a reference inside my CSS file that refers to a static image:

#logo
{
  background: url(\'/static/logo.png\')
}

This works just fi

相关标签:
8条回答
  • 2020-12-24 11:54

    Use absolute URL from base directory, this will point to any file in a static folder within an app

    settings.py:

    STATIC_URL = '/static/'
    

    style.css:

    background-image: url('/static/img/sample.jpg');
    
    0 讨论(0)
  • 2020-12-24 11:57

    See this similar stackoverflow question.

    The only way to do what you want is to generate your CSS through Django. HTML is usually associated with Django views and templates, but in truth, you can return any file type: CSS, JavaScript, plain text, etc. However, doing so will add overhead to your site, so setting proper HTTP headers and server-side caching of the generated file will be very important.

    Basic method:

    return render_to_response('stylesheet.css',
        { 'domain': 'http://static.mydomain.com/' },
        context_instance=RequestContext(request),
        mimetype='text/css'
    )
    

    Alternatively, you can set up hosts on your system that map the static domains back to localhost for development purposes. Then, you can reference the domain as normal, but it'll still pull from your development files. Also, if you happen to have Ruby installed on your system, you can make use of a rubygem called Ghost. It lets you easily create, enable, disable, and delete custom hosts right from the command-line with no fuss.

    0 讨论(0)
提交回复
热议问题