zoukankan      html  css  js  c++  java
  • django学习笔记(六)

    1)django上传图片是利用FileField和ImageField对象的。FileField和ImageField会将上传的数据保存在settings.py里定义的MEDIA_ROOT中的一个子目录下,它会在upload_to变量参数里定义。如果我们设置MEDIA_ROOT = "/var/www/gallery/media/",而upload_to为'photo'的话,那么最终照片就会被保存在/var/www/gallery/media/photos/里,如果这个目录不存在的话,就要先创建它,另外Web服务器运行的用户或者 用户组需要有对这个目录的写权限。

    class Photo(model.Model):

    >> title = models.CharField(max_length=100)

    >> image = models.ImageField(upload_to = 'photos')

    需要注意的是,我们需要安装一个特殊的Python类库PIL(Python Imaging Library)才能使用ImageField。PIL是一个常用的Python类库,它能处理各种各样的图片格式。可以在http://www.pythonware.com/products/pil下载源码包。

    2)用django处理ajax请求,返回JSON数据。

    from django.http import HttpResponse

    from django.core import serializers

    from xxxx import ABC

    def abc(request,id):

    >> response = HttpResponse()

    >> response['Content-Type'] = "text/javascript"

    >> response.write(serializers.serialize("json",ABC.objects.filter(pk_gt=id)))

    >> return response

    3)django时区设置。

    TIME_ZONE = 'GMT-8'               # 或者 'Asia/Shanghai'

  • 相关阅读:
    Go 打印出结构化结构体
    GOPROXY设置
    python判断链表是否有环
    单链表python和go的代码
    mongo索引
    python修改srt字幕的时间轴
    python各个版本的排序
    mac使用python识别图形验证码
    selenium运行js代码笔记
    布隆过滤器
  • 原文地址:https://www.cnblogs.com/cly84920/p/4426677.html
Copyright © 2011-2022 走看看