1

如果我可以将这个 jQuery 文件上传器添加到我的 Django 管理面板中,那就太棒了,但我是 Django 新手,我不确定从哪里开始。有人可以给我一些想法,并在这里为我指明正确的方向吗?

4

2 回答 2

1

我认为这对你很有用。您可以将其嵌入到模板文件中,例如:

{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="/static/js/admin/app/model/uploadjq.js"></script>
{% endblock %}
于 2012-09-18T10:00:40.823 回答
0

您可以通过下一个命令安装包 django-admin-multiupload,它会添加到 django admin 一个基于 jQuery File Upload 的表单:

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

或者只是从https://github.com/gkuhn1/django-admin-multiupload.git克隆到您的项目 中使用示例:

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)

Gallery 和 Image 模型在哪里:

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]
于 2013-10-27T19:11:55.803 回答