简体   繁体   中英

stylesheet not loading from relative path in django

Currently have a folder structure set up in django:

{appname}\\templates\\

{appname}\\templates\\assets\\css\\

but when I try to include something like <link rel="stylesheet" href="assets/css/bootstrap.css" /> in my templates in {appname}\\templates\\ , it's not loading for whatever reason; I can load directly from twitter, but I'd prefer to keep relative paths if possible.

Why might this not work? If it's any clue, the subfolders in PyCharm are appearing as light grey, not sure why that is happening either.


edit : okay so I read up here on how to set up static files, and have set up a static directory under my main project folder, with a css folder underneath that.

I updated settings.py to include:

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

# Additional locations of static files
import os.path

STATICFILES_DIRS = (
    os.path.dirname(__file__).replace('\\','/'),
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

and then attempted to add

<link rel="stylesheet" href="{{ STATIC_URL }}css/bootstrap.css" />

to my html file, but it's not loading -- what am I doing wrong here?

You want to keep your css in a folder called static in the root of you app. Then makes sure you have django.contrib.staticfiles incluced included in INSTALLED_APPS (within your settings.py). Now when you run python manage.py runserver , you're static files will be available at localhost:8000/static/ (this is defined by the STATIC_URL setting - which defaults to static/ ).

You'll need to make sure that you're using a RequestContext object in your view. This ensures that variables from settings.py (such as STATIC_URL) are available in the template -

def your_view(request):
    #...
    return render_to_response('page.html', context_data, context_instance=RequestContext(request))

(this is handled for you if you use the new render() shortcut instead of render_to_response )

If you're serving files in a production environment then you use the python manage.py collectstatic command to move all your static files (that's anything in a folder called static/ in the root of your app, plus anything in your STATICFILES_DIR ), to the folder defined by STATIC_ROOT . Then have your webserver serve these files at STATIC_URL (this isn't handled by django, and in fact the django docs seem to reccomend that you don't even use the same webserver that's serving the django app).

You can put your static files anywhere you want, and have django manage them, simply include the location of the directory in the STATICFILES_DIR tuple.

Have a look at the docs on serving static files. (I've assumed you're using django 1.4)

You should not put media files in a template directory. Use a media directory (in this case static) per app so your settings file can find it, see Django directory structure?

See also Confusion in Django admin, static and media files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM