A django app for upload avatars
IE6 Compatible
You can find a showcase at youtube or youku
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
It's better that you study the example
django project first.
where to store the uploaded images. Absolute path.
where to store the cropped avatars. Absolute path.
URL prefix for the original uploaded image, used for <img src="" />
display the uploaded image
for select area and crop. e.g. uploadedimage/
.
URL prefix for the real avatars. For display avatars in web page.
e.g. avatar/
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.
Because upload_avatar
app does not know how to get uid
and 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.
In the upload avatar web page, you should pass the uploadavatar_context to template.
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)
)
In you upload.html
template, Just simply do this:
{% include "upload_avatar/upload_avatar.html" %}
upload_avatar
app needs jQuery, So ensure your app/site contains jquery
Details see the example
django project
Max size allow upload. default is 3MB
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
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.
How many different sizes you wanna to resize.
NOTICE, the value must be list, even if there are only one size.
Default is: [50,]
Avatar default size which will be shown in you website,
this is for call user.get_avatar_path()
, user.get_avatar_url()
more convenient
default png
default 90
Whethe delete the uploaded original image after avatar cropped, default is True
this two controls the web layout and texts shown on web or javascript alert
All settings and Details, Default Values see app_settings.py