Giter Club home page Giter Club logo

django-qiniu's Introduction

django-qiniu

This package interages Qiniu cloud storage with Django framework. You must install qiniu's python-sdk before using this package.

Configuration

Add below lines in your django project's settings.py:

QINIU_ACCESS_KEY = 'your_key'
QINIU_SECRET_KEY = 'your_secret'
QINIU_BUCKET_DEFAULT = 'your_bucket_name'

How To Use

Use django-qiniu in your django project is very simple. First of all, your must copy django_qiniu directory to your PYTHON_PATH so you can import it.

1. Add Field To Your Model

To get started, you should add QiniuFileField or QiniuImageField to your models.py.

from django.db import models
from django_qiniu.fields import QiNiuImageField, QiniuFileField

def qiniu_key_maker_file(instance, filename):
    """

    Args:
    ~~~~~

    - instance, your model instance being saved
    - filename, original filename

    This function should return a string object which is the key of qiniu.
    """

def qiniu_key_maker_user_image(instance, filename):
    pass

class Photo(models.Model):
    qiniu_file = QiNiuFileField(upload_to=qiniu_key_maker_file, null=True)
    qiniu_image = QiNiuImageField(upload_to=qiniu_key_maker_image, null=True)

QiniuFileField's __init__ method receives some parameters:

  • upload_to, a function to generate qiniu file key.
  • upload_bucket, bucket_name, if not given, will use settings.QINIU_BUCKET_DEFAULT as default.
  • domain, your customized qiniu static domain, will use ('%s.qiniudn.com' % upload_bucket) as default if not given.

QiniuImageField is a subclass of QiniuFileField and designed to store especially image files.

2. Save your uploaded file in views.py

Save your uploaded file to qiniu server is very simple:

f = request.FILES['file']
obj = Photo()
obj.qiniu_file = f
# Calling save method will upload this file to qiniu server and requires network
# connection
obj.save()

How to use saved qiniu instance?

QiniuFileField

You can get the file url by: instance.qiniu_file.url

QiniuImageField

For QiniuImageField, There is a convient method called get_image_view which is used to get thumbnails of your images, such as:

instance.qiniu_image.get_image_view(mode=1, width=280, height=280, quality=75)
instance.qiniu_image.get_image_view(mode=2, width=640, quality=75)

For more information, visit(http://docs.qiniu.com/api/v6/image-process.html)

django-qiniu's People

Contributors

piglei avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.