Django-Upload-Avatar

宁宁123韩 发布于 2013/04/16 10:31
阅读 917
收藏 0
jango-Upload-Avatar#头像上传APP

A django app for upload avatars

IE6 Compatible #兼容 IE6

You can find a showcase at youtube or youku # 在优酷上可以找到演示地址
Install#安装方法

pip install django-upload-avatar

or clone git repo

git clone https://github.com/yueyoum/django-upload-avatar.git
cd django-upload-avatar
python setup.py install# 好像如果用setup.py安装有点错误

Usage#使用方法

It's better that you study the example django project first.
#通过django 项目的第一个例子可以让你更好的学习

add upload_avatar in your INSTALLED_APPS
#在settings.py中的INSTALLED_APPS添加appupload_avatar、

add url(r'', include('upload_avatar.urls')) in your project's urls.py
在url.py中添加你项目的url地址

Necessary settings#一些必要的配置

UPLOAD_AVATAR_UPLOAD_ROOT

where to store the uploaded images. Absolute path.#在何处存放上传的图像。绝对路径。
UPLOAD_AVATAR_AVATAR_ROOT

where to store the cropped avatars. Absolute path.#在哪里保存裁剪后的相片。绝对路径。
UPLOAD_AVATAR_URL_PREFIX_ORIGINAL

URL prefix for the original uploaded image, used for <img src="" /> display the uploaded image for select area and crop. e.g. uploadedimage/.#原始上传图像的URL前缀,使用<img src="" />来陈列你上传你的剪切的图片

UPLOAD_AVATAR_URL_PREFIX_CROPPED

URL prefix for the real avatars. For display avatars in web page. e.g. avatar/URL地址显示真是的头像,成列在网页上的图片例如头像之类的
In you app
models.py

The simplest demo as the example/app/models.py

from upload_avatar.signals import avatar_crop_done
from upload_avatar.models import UploadAvatarMixIn

class User(models.Model, UploadAvatarMixIn):
    user = models.ForeignKey('auth.User', related_name='user_info')
    avatar_name = models.CharField(max_length=128)

    def get_uid(self):
        return self.user.id

    def get_avatar_name(self, size):
        return UploadAvatarMixIn.build_avatar_name(self, self.avatar_name, size)


def save_avatar_in_db(sender, uid, avatar_name, **kwargs):
    if User.objects.filter(user_id=uid).exists():
        User.objects.filter(user_id=uid).update(avatar_name=avatar_name)
    else:
        User.objects.create(user_id=uid, avatar_name=avatar_name)

avatar_crop_done.connect(save_avatar_in_db)

your user models MUST define a field that store the avatar name, and inherit from UploadAvatarMixIn and define get_uid, get_avatar_name methods.
#您的用户模型必须定义一个字段存储的头像名称,并继承从UploadAvatarMixIn和定义方法get_uid,get_avatar_name。
Because upload_avatar app does not know how to get uid and avatat_name
#因为上传头像的应用程序不知道如何获得uid和avatat_name
Also, you should define a function which like save_avatar_in_db in the example. and make avatar_crop_done signal connected with this function.#此外,你应该定义一个函数在这个例子像save_avatar_in_db。,使头像信息做信号连接使用此功能。
views.py

In the upload avatar web page, you should pass the uploadavatar_context to template.
#在上传头像的网页,你应该通过uploadavatar_context的模板。
demo:

from upload_avatar import get_uploadavatar_context

@login_required
def upload(request):
    return render_to_response(
        'upload.html',
        get_uploadavatar_context(),
        context_instance = RequestContext(request)
    )

templates

In you upload.html template, Just simply do this:

{% include "upload_avatar/upload_avatar.html" %}

jQuery.js

upload_avatar app needs jQuery, So ensure your app/site contains jquery

Details see the example django project
Optional settings
UPLOAD_AVATAR_MAX_SIZE

Max size allow upload. default is 3MB
UPLOAD_AVATAR_TEST_FUNC

this function controls whether the request to upload/crop view function is valid.

this function take request as the only argument, return True means there is a valid request, otherwise this request is forbidden.

default is:

UPLOAD_AVATAR_TEST_FUNC = lambda request: request.method == 'POST' and \
                          request.user.is_authenticated()

If you are using custom user system, or has some other stuff to test, define your own test func
UPLOAD_AVATAR_GET_UID_FUNC

upload_avatar app does not know how to get uid from request object.

default is

UPLOAD_AVATAR_GET_UID_FUNC = lambda request: request.user.id

as same as UPLOAD_AVATAR_TEST_FUNC, If you are using custom user system, maybe you should define this func.
UPLOAD_AVATAR_RESIZE_SIZE

How many different sizes you wanna to resize.

NOTICE, the value must be list, even if there are only one size.

Default is: [50,]
UPLOAD_AVATAR_DEFAULT_SIZE

Avatar default size which will be shown in you website, this is for call user.get_avatar_path(), user.get_avatar_url() more convenient
UPLOAD_AVATAR_SAVE_FORMAT

default png
UPLOAD_AVATAR_SAVE_QUALITY

default 90
UPLOAD_AVATAR_DELETE_ORIGINAL_AFTER_CROP

Whethe delete the uploaded original image after avatar cropped, default is True
UPLOAD_AVATAR_WEB_LAYOUT
UPLOAD_AVATAR_TEXT

this two controls the web layout and texts shown on web or javascript alert

All settings and Details, Default Values see app_settings.py


坑爹的我连django-upolad 都装不上去  我擦  ~~~ 配置的我晕头转向的  谁用过这个能把文档给俺看看吗?

加载中
返回顶部
顶部