1

It would be really awesome if I could add this jQuery file uploader to my Django admin panel, but I'm new to Django and I'm not really sure where to start. Could someone give me some ideas, and point me in the right direction here?

5

2 Answers 2

1

I think this is useful for you. You can embed it in template file, such as:

{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="/static/js/admin/app/model/uploadjq.js"></script>
{% endblock %}
Sign up to request clarification or add additional context in comments.

Comments

0

You can install package django-admin-multiupload, which is added into the django admin a form based on jQuery File Upload, by next command:

pip install git+git://github.com/gkuhn1/django-admin-multiupload.git

Or just clone into your project from https://github.com/gkuhn1/django-admin-multiupload.git Example of usage:

from django.contrib import admin
from django.shortcuts import get_object_or_404

from gallery.models import Gallery, Image

from multiupload.admin import MultiUploadAdmin

class ImageInlineAdmin(admin.TabularInline):
    model = Image


class GalleryMultiuploadMixing(object):

    def process_uploaded_file(self, uploaded, gallery, request):
        if gallery:
            image = gallery.images.create(file=uploaded)
        else:
            image = Image.objects.create(file=uploaded, gallery=None)
        return {
            'url': image.file.url,
            'thumbnail_url': image.file.url,
            'id': image.id,
            'name': image.filename
        }

class GalleryAdmin(GalleryMultiuploadMixing, MultiUploadAdmin):
    inlines = [ImageInlineAdmin,]
    multiupload_form = True
    multiupload_list = False

    def delete_file(self, pk, request):
        '''
        Delete an image.
        '''
        obj = get_object_or_404(Image, pk=pk)
        return obj.delete()


class ImageAdmin(GalleryMultiuploadMixing, MultiUploadAdmin):
    multiupload_form = False
    multiupload_list = True


admin.site.register(Gallery, GalleryAdmin)
admin.site.register(Image, ImageAdmin)

Where Gallery and Image models are:

from django.db import models

# Create your models here.

class Gallery(models.Model):
    class Meta:
        verbose_name_plural = 'Galleries'
    title = models.CharField('Title', max_length=20)

    def __str__(self):
        return self.title


class Image(models.Model):
    file = models.FileField('File', upload_to='images/')
    gallery = models.ForeignKey('Gallery', related_name='images', blank=True, null=True)

    def __str__(self):
        return self.filename

    @property
    def filename(self):
        return self.file.name.rsplit('/', 1)[-1]

1 Comment

django-admin-multiupload worked for me for basic admin support of multiple file upload using the jquery file upload plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.