GithubHelp home page GithubHelp logo

richardrowe / django-awesome-avatar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from voronind/django-awesome-avatar

0.0 2.0 0.0 202 KB

Django Avatar field

Python 17.89% CSS 7.53% JavaScript 74.58%

django-awesome-avatar's Introduction

django-awesome-avatar

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',
    ...
}

django-awesome-avatar's People

Contributors

ellmetha avatar richardrowe avatar vkorchagin avatar voronind avatar

Watchers

 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.