dima-kov / django-croppie

Django application for croppie.js integration
MIT License
26 stars 11 forks source link

Render croppie again #12

Open yash-malviya opened 4 years ago

yash-malviya commented 4 years ago

Hi ! i am using croppie and its working fine. I was trying to add some functionality like clicking on a button gives me the cropped image [working fine] but i also wanted to add a button which takes be back to crop the same uploaded image. is there a way to render croppie again without actually refreshing the page. ThankYou. ;)

dima-kov commented 4 years ago

I need more details to say smth. Could you paste the code to reproduce?

is there a way to render croppie again without actually refreshing the page.

I think, yes, you could do this with js, but, once more, I need more details.

dima-kov commented 4 years ago

Also, take a look on this fork: https://github.com/strycore/django-croppie

yash-malviya commented 4 years ago

Sorry but i am no longer working on that. but i do need help in Saving the image

class FiraCreateView(CreateView):
    model = Fira
    form_class = FiraForm
    template_name = 'create.html'
    success_url = '/'

above view is given in the example file

but i want the same functionality [saving the cropped image to my models] using something like this

def home(request):
    if request.method == 'POST':
        # i want to get the cropped image here and save it to the model
        else:
            return HttpResponse('<h1> Opps ! An Error ! Form rejected ..  ;( </h1>')
    if request.method == 'GET':
        form = CroppieImageForm()
        posts = 
        profile =
        return render(request, 'User/home.html', {
            'posts': posts,
            'profile': profile,
            'form': form
        })

So the reason why i wanna do something like this is because i m not adding a new row in my database my profile database already exists and i only wanna update the picture field.

Models.py

class Profile(models.Model):
    CHOICES = [
        ('Male', 'Male'),
        ('Female', 'Female'),
    ]
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=20)
    full_name = models.CharField(max_length=50)
    dob = models.DateField()
    picture = models.ImageField(upload_to='ProfileImages/',  blank=True)
    gender = models.CharField(max_length=6, choices=CHOICES)

    class Meta:
        db_table = 'profile'

    def __str__(self):
        return self.full_name

Forms.py

class CroppieImageForm(forms.ModelForm):
    image = CroppieField(options={
            'viewport': {
                'width': 250,
                'height': 250,
                'type': 'circle',
            },
            'boundary': {
                'width': 500,
                'height': 300,
            },
            'showZoomer': False,
        }, label=''
    )

    class Meta:
        model = Profile
        fields = ('picture',)
shalltell commented 2 years ago

Also, take a look on this fork: https://github.com/strycore/django-croppie

I added this as a PR. It has conflicts, but have tested with -X theirs strategy without issues.

dima-kov commented 2 years ago

Regarding the view:

def home(request):
    if request.method == 'POST':
        form = CroppieImageForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            form.save()
            return redirect('/success')
        else:
            return HttpResponse('<h1> Opps ! An Error ! Form rejected ..  ;( </h1>')
    if request.method == 'GET':
        form = CroppieImageForm()
        posts = 
        profile =
        return render(request, 'User/home.html', {
            'posts': posts,
            'profile': profile,
            'form': form
        })