简体   繁体   English

样式表未从Django的相对路径加载

[英]stylesheet not loading from relative path in django

Currently have a folder structure set up in django: 当前在django中设置了文件夹结构:

{appname}\\templates\\ {appname} \\ templates \\

{appname}\\templates\\assets\\css\\ {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; 但是,当我尝试在{appname}\\templates\\模板中包含诸如<link rel="stylesheet" href="assets/css/bootstrap.css" />之类的内容时,无论出于何种原因都不会加载; I can load directly from twitter, but I'd prefer to keep relative paths if possible. 我可以直接从twitter加载,但如果可能的话,我希望保留相对路径。

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. 如果有任何线索,PyCharm中的子文件夹将显示为浅灰色,不知道为什么会发生这种情况。


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. 编辑 :好的,所以我在这里阅读了有关如何设置静态文件的信息,并在我的主项目文件夹下建立了一个static目录,并在其下面有一个css文件夹。

I updated settings.py to include: 我更新了settings.py以包括:

# 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? 到我的html文件,但未加载-我在这里做错了什么?

You want to keep your css in a folder called static in the root of you app. 您想将css放在应用程序根目录中名为static的文件夹中。 Then makes sure you have django.contrib.staticfiles incluced included in INSTALLED_APPS (within your settings.py). 然后确保INSTALLED_APPS (在settings.py中)包含了django.contrib.staticfiles 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/ ). 现在,当您运行python manage.py runserver ,您可以在localhost:8000 / static /中使用静态文件(这由STATIC_URL设置定义-默认为static/ )。

You'll need to make sure that you're using a RequestContext object in your view. 您需要确保在视图中使用RequestContext对象。 This ensures that variables from settings.py (such as STATIC_URL) are available in the template - 这样可确保模板中的settings.py中的变量(例如STATIC_URL)可用-

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 ) (如果您使用新的render()快捷方式而不是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 . 如果要在生产环境中提供文件,则可以使用python manage.py collectstatic命令将所有静态文件(即应用程序根目录中名为static/的文件夹中的任何内容,以及STATICFILES_DIR中的STATICFILES_DIR )移动到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). 然后让您的网络服务器在STATIC_URL提供这些文件(django不会处理,实际上django文档似乎建议您甚至不使用与服务django应用程序相同的网络服务器)。

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. 您可以将静态文件放置在所需的任何位置,并由django对其进行管理,只需在STATICFILES_DIR元组中包含目录的位置即可。

Have a look at the docs on serving static files. 看一看在文档上提供静态文件。 (I've assumed you're using django 1.4) (我假设您正在使用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? 每个应用使用一个媒体目录(在这种情况下为静态),以便您的设置文件可以找到它,请参阅Django目录结构?

See also Confusion in Django admin, static and media files 另请参阅Django管理,静态和媒体文件中的混淆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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