V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lbfeng
V2EX  ›  Python

如何在衍生模版中引用新的 css 文件?

  •  
  •   lbfeng · 2015-07-07 06:27:59 +08:00 · 2026 次点击
    这是一个创建于 3232 天前的主题,其中的信息可能已经有所发展或是发生改变。

    base.html
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">

    new.html
    {% extends "base.html" %}
    这个页面中引用另一个的css而不是'style.css',直接引用会用冲突,有什么办法解决呢?

    4 条回复    2015-07-08 15:54:20 +08:00
    Septembers
        1
    Septembers  
       2015-07-07 06:53:26 +08:00   ❤️ 1
    1.原则上CSS尽可能不要污染标签的样式 另外 命名尽可能完备
    2.如果在不改变现有模板组织结构的前提下的话
     可以用JavaScript干涉head中的样式引用来达到目的

    @Livid @Kai This may be moved to /go/python
    virusdefender
        2
    virusdefender  
       2015-07-07 07:36:38 +08:00   ❤️ 1
    在base里面增加一个block 比如{% block cssblock %}{% endblock %] 然后在后面的模板中覆盖掉
    P233
        3
    P233  
       2015-07-07 08:49:44 +08:00   ❤️ 1
    在原 CSS 文件中 @import
    cc7756789
        4
    cc7756789  
       2015-07-08 15:54:20 +08:00   ❤️ 1
    我觉得你可以在页面的视图中传个boolean, 在base.html做个判断,
    @app.route('/new')
    def new():
    var = True


    {% if var %}
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='newcss') }}">
    {% elif %}
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='styles.css') }}">
    {% endif %}
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   959 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:22 · PVG 06:22 · LAX 15:22 · JFK 18:22
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.