Django:在基于类的视图中包含媒体(css / js) [英] Django: Include Media (css/js) in Class-Based Views

查看:95
本文介绍了Django:在基于类的视图中包含媒体(css / js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将旧的django代码从基于方法的视图更新为基于类的视图。

I am updating old django code from method-based views to class-based views.

我知道如何通过表单将媒体(css / js)包含在表单中媒体类

I know how to include media (css/js) in forms via the Media class

如果基于类的视图不包含任何形式,该如何使用媒体类?

How can I use the media class if my class based view does not contain any forms?

推荐答案

CSS / JS通常在模板本身而非视图中进行管理。参见 https://docs.djangoproject.com/en/1.10/howto/static -files /

CSS/JS are usually managed in the template itself and not in the view. See https://docs.djangoproject.com/en/1.10/howto/static-files/

例如,使用base.html:

For example, use base.html:

<!DOCTYPE html>
<html>
    <head>

        <title>
            {% block page_title %}{{ page_title }}{% endblock %}
        </title>

        {% block css %}
        {% endblock %}

    </head>
    <body>

        {% block main %}
        {% endblock %}

        {% block scripts %}
        {% endblock %}

    </body>
</html>

并使用my_page.html对其进行扩展:

and extend it with my_page.html:

{% extends "base.html" %}
{% load staticfiles %}

{% block page_title %}
Hello!
{% endblock %}

{% block css %}
    <link href="{% static "page.css" %}" rel="stylesheet"/>
{% endblock %}

{% block main %}
Yo!
{% endblock %}

{% block scripts %}
    <script src="{% static 'my_scripts.js' %}"></script>
{% endblock %}

这篇关于Django:在基于类的视图中包含媒体(css / js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆