[Django基础11]网页中的css/js/image处理

1. 页面样式处理

通常情况下,网页开发时,页面中的样式是通过外部css样式进行处理的,外部的css文件加载在Django中,需要进行简单的处理

首先去到我们的mysite/mysite/settings.py配置文件中,包含了STATIC_URL='/static/'的配置(默认是已经配置的)

我们在应用模块文件夹下,创建一个目录static/专门用于存放css样式文件,创建一个目录images/专门保存页面中用到的图片,创建一个lib/专门用于保存第三方的文件(如jquery、bootstrap等等)

创建mysite/polls/static/style.css样式文件,用于修饰我们的首页index.html样式,编辑内容如下

*{margin:0;padding:0;}
h1{font-size:18px;color:#069;font-weight:bolder;}
ul{list-style:none;font-size:16px;}
ul > li{height:20px;line-height:20px;font-family: "微软雅黑"}

因为我们的项目是用在网络服务器上的,所以在mysite/polls/templates/index.html文件中引入样式,首先要通过{%load static%}获取static/文件夹的绝对路径,然后在页面中,使用{% static '路径'%}的形式来使用;

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    {% load static %}
    <link rel="stylesheet" href="{% static 'polls/style.css' %}">
    <title>投票模块首页</title>
    <link rel="stylesheet" href="">
</head>
<body>    
    <h1>投票模块首页</h1>
    ![]({% static 'polls/images/my.jpg'%})
    {%if question_list%}
    <ul>
        {%for question in question_list%}
        <li><a href="{%url 'polls:detail' question.id%}">{{question.question_text}}</a></li>
        {%endfor%}
    </ul>
    {%else%}
        <p>Congratulations,目前没有什么问题</p>
    {%endif%}
</body>
</html>

完成上述代码之后,我们打开网页访问应用的首页,就可以看到我们的样式和图片都加载OK了

网页中使用样式和图片

2. 使用第三方模块Bootstrap

进入http://www.bootcss.com下载bootstrap框架文件,这里我使用的是bootstrap3.4的版本。下载好之后,将解压得到的bootstrap文件中的css/、js/、font/三个文件拷贝到我们项目的mysite/polls/static/lib/文件夹下

bootstrap中文官网
项目目录结构
项目目录结构

接下来,我们编辑mysite/polls/templates/index.html页面,引入bootstrap并添加它的样式处理如下:

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    {% load static %}
    <link rel="stylesheet" href="{% static 'polls/lib/bootstrap3.4/css/bootstrap.min.css' %}">
    <title>投票模块首页</title>
    <link rel="stylesheet" href="">
</head>
<body>    
    <div class="container">
        <div class="row">
            <div class="page-header">
                <h1>用户问题列表 <small>解决方案提供平台</small></h1>
            </div>
        </div>
        <div class="row">
            {%if question_list%}
            <table class="table table-hover table-striped">
                <tr>
                    <th>问题编号</th>
                    <th>问题描述</th>
                    <th>操作</th>
                </tr>
                {%for question in question_list%}
                <tr>
                    <td>{{forloop.counter}}</td>
                    <td>{{question.question_text}}</td>
                    <td><a href="{% url 'polls:vote' question.id%}">查看详细</a></td>
                </tr>
                {%endfor%}
            </table>
            {%else%}
                <p>Congratulations,目前没有什么问题</p>
            {%endif%}
        </div>
    </div>
</body>
</html>

重新启动项目,我们打开浏览器访问项目首页,可以看到一个非常漂亮的首页呈现在了眼前

使用bootstap样式修饰的网页

来源:http://www.jianshu.com/p/53e2dfc74168

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值