python学习

Python学习打call第五十九天:Django模板静态文件处

2019-04-14  本文已影响12人  暖A暖

1.Django 对css静态文件的处理

我们先来看Django 对css静态文件是怎么处理的,一起来实现一下:

.header{
    color: red;
    font-size: 60px;
    background-color: black;
    height: 100px;
    text-align: center;
    line-height: 100px;
}
.content{
    color: green;
    font-size: 60px;
    background-color: #eeeeee;
    height: 800px;
    text-align: center;
    line-height: 800px;
}
.footer{
    color: green;
    font-size: 60px;
    background-color: black;
    height: 100px;
    text-align: center;
}
<!doctype html>
{% load staticfiles %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    # 这里使用到了Django的引擎中引用静态文件的模板语法
    # 引用的是static目录下的css/base.css文件
    <link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
       <div class="header">
        今天是星期几呀
       </div>

    <!-- 内容变化 -->
    {% block content %}
    <div class="content">
        <p>今天是星期一</p>
    </div>
    {% endblock %}

    <div class="footer">
        好的,谢谢
    </div>
</body>
</html>

2.Django 对image静态文件的处理

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"), )
< img src="{% static 'images/图片名称' %}" alt="说明">

3.Django 对用户上传图片的处理

Django 对于文件或图片上传在HTML页面中一般是通过form表单附件上传的,一般是使用form的post提交方式,编码类型为:"multipart/form-data";
form表单提交需要csrf token认证 如:{% csrf_token %};

4.图片文件等上传

自己选择一个app,我是在usersAPP中操作的:

url(r'^images/$', ImageView.as_view(), name='image');
class ImageView(View):
    def get(self, request):
        return render(request, 'image.html')
    def post(self, request):
        image = request.FILES.get('upload')
        image_instance = Image(name=image.name, image=image)
        image_instance.save()
        return render(request, 'image.html', {'msg': '图片上传成功'})
<!doctype html>
{% load staticfiles %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <form action="{% url 'users:image' %}" method="post" enctype="multipart/form-data">
{#        <input type="file" name="upload" accept="image/gif, image/jpeg, image/png, image/jpg">#}
        <input type="file" name="upload">
        <input type="submit" value="提交">
        {% csrf_token %}
    </form>
    <!-- 显示图片上传成功还是失败 -->
    <p> {{ msg }} </p>
</body>
</html>
class Image(models.Model):
    name = models.CharField(max_length=50, verbose_name="图片名称")
    image = models.ImageField(upload_to="images/%Y/%m", verbose_name="用户上传的图片", max_length=100)
    class Meta:
        verbose_name = "用户上传图片"
        verbose_name_plural = verbose_name
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

5.图片访问

url(r'^show_images/(?P<image_name>\w+.(png|jpg|jpeg))$', ShowImageView.as_view(), name='show_images')
class ShowImageView(View):
    def get(self, request, image_name):
        # 这是一个QuerySet
        images = Image.objects.filter(name=image_name)
        # 取第一张图片
        image = images.first()
        return render(request, 'show_image.html', {'image': image})

<!doctype html>
{% load staticfiles %}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <p>用户上传的图片为: </p>
    <!-- <img src="../../media/{{ image.image }}" rel="{{ MEDIA_URL }}{{ image.image }}" alt="用户上传图片"> -->
    <img src="{{ MEDIA_URL }}{{ image.image }}" alt="用户上传图片">
</body>
</html>
MEDIA_URL = '/media/'

'django.template.context_processors.media',

参考:https://www.9xkd.com/user/plan-view.html?id=2612520594

上一篇下一篇

猜你喜欢

热点阅读