将jQuery文件上载添加到Django管理员
如果我能添加到我的Django管理面板中,那将是非常棒的,但是我对Django是新手,我不确定从哪里开始。有人能给我一些想法,给我指出正确的方向吗?我想这对你很有用。您可以将其嵌入到模板文件中,例如:将jQuery文件上载添加到Django管理员,jquery,django,file-upload,admin,Jquery,Django,File Upload,Admin,如果我能添加到我的Django管理面板中,那将是非常棒的,但是我对Django是新手,我不确定从哪里开始。有人能给我一些想法,给我指出正确的方向吗?我想这对你很有用。您可以将其嵌入到模板文件中,例如: {% block extrahead %}{{ block.super }} <script type="text/javascript" src="/static/js/admin/app/model/uploadjq.js"></script> {% endblock
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="/static/js/admin/app/model/uploadjq.js"></script>
{% endblock %}
{%block extrahead%}{{block.super}
{%endblock%}
我认为这对您很有用。您可以将其嵌入到模板文件中,例如:
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="/static/js/admin/app/model/uploadjq.js"></script>
{% endblock %}
{%block extrahead%}{{block.super}
{%endblock%}
您可以通过下一个命令安装软件包django admin multiupload,该软件包基于jQuery文件上传添加到django admin a表单中:
pip install git+git://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]
您可以通过下一个命令安装程序包django admin multiupload,该程序包将添加到基于jQuery文件上载的django admin a表单中:
pip install git+git://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]
你试过了吗?看起来很有趣。你以前用过吗?我在让它工作时遇到了麻烦。不:)你有什么问题?为什么不试试呢?看起来确实很有趣。你以前用过吗?我在让它工作时遇到了问题(注:)你有什么问题?为什么不使用django admin multiupload为我提供了使用jquery文件上传插件的多文件上传的基本管理支持。django-admin-multiupload为我提供了使用jquery文件上传插件的多文件上传的基本管理支持。