如果我可以将this jQuery file uploader添加到我的Django管理面板中真的很棒,但我是Django的新手,我不确定从哪里开始。有人可以给我一些想法,并指出我在这方面的正确方向吗?
答案 0 :(得分:1)
我认为这对你有用。您可以将其嵌入模板文件中,例如:
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="/static/js/admin/app/model/uploadjq.js"></script>
{% endblock %}
答案 1 :(得分:0)
您可以安装软件包django-admin-multiupload,它将通过下一个命令添加到基于jQuery文件上载的django admin表单中:
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)
图库和图像模型的位置:
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]