2014-05-05 24 views
2

我不确定如何将CSS包含到我的html中。我的项目中有一个应用程序,它有自己的模板文件夹。我想把sample.css放到我的html中。在Django中导入应用程序css

在东西线<link rel="stylesheet" type="text/css" href="{% %}">

不是真的一定要放什么东西在{% %}

结构之间:

/proj 
    /app 
     /templates 
     sample.css 
     sample.html 

这里是我的TEMPLATE_DIRS:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,'templates'), 
    os.path.join(BASE_DIR,'home/templates'), 
    os.path.join(BASE_DIR,'gallery/templates'), 
    os.path.join(BASE_DIR,'contact/templates'), 
    os.path.join(BASE_DIR,'aboutme/templates'), 
) 

如果任何其他信息是必需的。请告诉我!

更新struture

/proj 
    /app 
     /static 
     /css 
      sample.css 
     /templates 
     sample.html 

回答

2

css文件,以及js文件和图片,被认为是static files

通常的做法是使用django.contrib.staticfiles内置Django应用程序来管理静态文件。

如果设置了应用程序正确,这是怎样的link会是什么样子:

<link rel="stylesheet" type="text/css" href="{% static 'sample.css' %}"> 
+0

@Liondancer你也应该把'css'文件放在其他文件夹中。它是将它们放在名为'app/static/css'的文件夹中的一种常见模式。 –

+0

@RaydelMiranda,谢谢你的提示。 – alecxe

+0

@RaydelMiranda如此重命名我的模板为静态? – Liondancer

2

为@alecxe提到,他们有一个命名为静态的文件夹中,包括这些在你的设置文件。

STATIC_URL = '/static/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    # 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. 
) 

# List of finder classes that know how to find static files in 
# various locations. 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 
相关问题