python django怎么添加css

mh_memory 2014-10-06 04:23:39
网上的方法各种不行.
我的目录情况:
helloworld/
--------------_init_.py
--------------setting.py
--------------urls.py
--------------views.py
--------------templates/
----------------------------index.html
----------------------------css/
----------------------------------common.css

然后urls.py添加了(r'^index/$', show_index),后在http://127.0.0.1:8000/index/可以访问到index.html页面,但是index.html中的css却没有作用。index.html中引入css的语句为:
<link type="text/css" rel="stylesheet" href="./css/common.css" />

请教大神django怎么添加这个common.css?
...全文
472 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq373016067 2014-10-15
  • 打赏
  • 举报
回复
玥-环玦 2014-10-14
  • 打赏
  • 举报
回复
主要就是 在 setting里 导入css的模板 不然用不了
Sunhk_25 2014-10-09
  • 打赏
  • 举报
回复
学习。。
mh_memory 2014-10-08
  • 打赏
  • 举报
回复
引用 4 楼 mh_memory 的回复:
多谢各位大神,现在可以了、 把方法贴出来供后人参考: 路径: helloworld/ ---------------init.py ---------------views.py ---------------settings.py ---------------urls.py ---------------templates/ -----------------------------index.html ---------------static/ -----------------------css/ -----------------------------common.csss -----------------------js/ --------------------------common.js 办法: 1. settings.py最下方STATIC_URL下面补上 STATIC_ROOT = os.path.join(os.path.dirname(__file__),'static') STATICFILES_DIRS = ( ('css',os.path.join(STATIC_ROOT,'css').replace('\\','/') ), ('js',os.path.join(STATIC_ROOT,'js').replace('\\','/') ), ('images',os.path.join(STATIC_ROOT,'images').replace('\\','/') ), ('upload',os.path.join(STATIC_ROOT,'upload').replace('\\','/') ), ) 2. urls.py文件补以下两行 import settings (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), 3.html引用办法 <link type="text/css" rel="stylesheet" href="/static/css/common.css" /> <script type="text/javascript" src="/static/js/common.js" ></script>
第2点可以不用设置 #2. urls.py文件补以下两行 #import settings #(r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
mh_memory 2014-10-08
  • 打赏
  • 举报
回复
多谢各位大神,现在可以了、 把方法贴出来供后人参考: 路径: helloworld/ ---------------init.py ---------------views.py ---------------settings.py ---------------urls.py ---------------templates/ -----------------------------index.html ---------------static/ -----------------------css/ -----------------------------common.csss -----------------------js/ --------------------------common.js 办法: 1. settings.py最下方STATIC_URL下面补上 STATIC_ROOT = os.path.join(os.path.dirname(__file__),'static') STATICFILES_DIRS = ( ('css',os.path.join(STATIC_ROOT,'css').replace('\\','/') ), ('js',os.path.join(STATIC_ROOT,'js').replace('\\','/') ), ('images',os.path.join(STATIC_ROOT,'images').replace('\\','/') ), ('upload',os.path.join(STATIC_ROOT,'upload').replace('\\','/') ), ) 2. urls.py文件补以下两行 import settings (r'^static/(?P.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}), 3.html引用办法 <link type="text/css" rel="stylesheet" href="/static/css/common.css" /> <script type="text/javascript" src="/static/js/common.js" ></script>
码无边 2014-10-08
  • 打赏
  • 举报
回复
static 下新建 css目录
码无边 2014-10-08
  • 打赏
  • 举报
回复
首先你要把css目录添加到模板 示例 settings.py STATIC_URL = '/static/' STATIC_ROOT = 'D:/wamp/www/django_web/mysite/mysiteapp/static'; TEMPLATE_DIRS = ( #os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'), #os.path.join(HERE,'templates'), 'D:/wamp/www/django_web/mysite/mysiteapp/templates', # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". # Always use forward slashes, even on Windows. # Don't forget to use absolute paths, not relative paths. )
panghuhu250 2014-10-06
  • 打赏
  • 举报
回复
看django有关static file管理的文档.

37,722

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧