Skip to main content

Django Avatar field

Project description

Django-awesome-avatar is a reusable application providing Avatar model field. It allows crop selected area before saving image.

Purpose

  • field in profile model instead creating model for saving images

  • HTML5 File API instead hidden iframe AJAX for image preview

  • easy customizable presence (any view and widget templates)

Install

To integrate django-awesome-avatar with your site, there are few things that are required:

  1. Installing:

    pip install django-awesome-avatar
  2. List this application in the INSTALLED_APPS portion of your settings file. Your settings file will look something like:

    INSTALLED_APPS = (
        ...
        'awesome_avatar',
    )

Usage examples

with ModelForm

Add the AvatarField to your user or profile model:

from awesome_avatar.fields import AvatarField

class Profile(Model):
    user = OneToOneField(User, related_name='profile')
    ...
    avatar = AvatarField(upload_to='avatars', width=100, height=100)

Use model form usually way:

class AvatarChangeForm(ModelForm):
    class Meta:
        model = Profile
        fields = ['avatar']

def change_avatar(request):
    if request.method == 'POST':
        form = AvatarChangeForm(request.POST, request.FILES,
                                instance=request.user.profile)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/profile/')
    else:
        form = AvatarChangeForm(instance=request.user.profile)

    return render(request, 'template.html', {'form': form})

with Form

Define some model for saving images:

class Images(Model):
    image = ImageField(upload_to='images')

Use form field for cropping image:

from awesome_avatar import forms as avatar_forms

class UploadAndCropImageForm(Form):
    image = avatar_forms.AvatarField()

def upload_and_crop_image(request):
    if request.method == 'POST':
        form = UploadAndCropImageForm(request.POST)

        if form.is_valid():
            Images(image=form.image).save()
            return HttpResponseRedirect('/any/')
    else:
        form = UploadAndCropImageForm()

    return render(request, 'template.html', {'form': form})

Global Settings

Django’s settings.py:

AWESOME_AVATAR = {
    'width': 100,
    'height': 100,

    'select_area_width': 400,
    'select_area_height': 300,

    'save_quality': 90,
    'save_format': 'png',
    ...
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-awesome-avatar-1.1.3.tar.gz (58.6 kB view details)

Uploaded Source

File details

Details for the file django-awesome-avatar-1.1.3.tar.gz.

File metadata

File hashes

Hashes for django-awesome-avatar-1.1.3.tar.gz
Algorithm Hash digest
SHA256 f21efe208db74e78abcaa4bcf47ba95419db5ba7babc936d2561db655488910c
MD5 1a3d7db58fe03c7be553d9e778d2b627
BLAKE2b-256 b1a0e79f0e0d1190e8fe24ac22cc948176de80c55d9dda56862ea661e6fafdd4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page