django管理员上传文件的后处理 [英] django admin post processing of uploaded file
问题描述
我有一个标准的Django管理页面,用于上传多个文件。我想执行以下操作:
- 直接上传一些文件
- 一个文件需要在使用AES进行加密(加密可以通过python或通过http到加密服务器加密)。
- 需要解压缩,处理和重新打包的zip文件。 >
我现在只有一个非常基本的管理页面。任何人都可以指向正确的方向,从哪里开始?请指出我需要修改哪个文件,因为我仍然不熟悉django。
只是一个简短的方向将不胜感激。谢谢。
我没有测试这些代码,但我可以直接指导你从哪里开始。
我建议你在模型的保存功能中编写解压缩的代码。这是最简单的方法,但不是最好的。
Django管理员可以处理多个表单作为自定义django管理员。
我希望你的模型有点像这些
来自django.db导入模型的$ $ $ $ $ $
从django.core.files.storage导入FileSystemStorage
fs = FileSystemStorage(location =/ var / www / yoursite / private /)
class SetOfFiles(models.Model):
name = models.CharField('set name'),max_length = 225,null = False,blank = F $)
class File(models.Model):
set = models.ForeignKey(SetOfFiles,verbose_name = _('set'))
file = models.FileField = fs)
def save(self,* args,** kwargs):
如果不是self.id:
...解压缩你的文件...
...加密你的文件,如果需要的话...
super(File,self).save(* args,** kwargs)
在相关应用程序中创建admin.py以自定义管理员来处理多个插入:
从d jango.contrib import admin
class FileInline(admin.TabularInline):
model = File
class SetOfFilesAdmin(admin.ModelAdmin):
list_display =('name',)
inlines = [FileInline]
admin.site.register(SetOfFiles,SetOfFilesAdmin)
这里也是解压缩FileBrowser 代码上传的.zip文件,但由于使用FileBrowser应用程序,可能会有点复杂。您可以使用 zipfile python模块解压缩文件。
此外,您可以在AES加密中使用 PyCrypto 。
I have a standard Django Admin page that is used to upload multiple files. I wish to do the following:
- upload some of the files directly
- One file need to be encrypted with AES before save (encryption can be done by python or through http to an encryption server.)
- A zip file needed to be unzip, process and re-package.
I only have a very basic admin page now. Can anyone point me to the right direction in where to start with? Please point me exactly which file i need to modified as i am still unfamiliar with django.
Just a brief direction will be appreciated. Thank you.
I didn't test these code, but I can just direct you where to start. I would suggest you to write the code of unzipping at the model's save function. This is the easiest way but not the best. Django admin can handle multiple form as customizing django admin.
I hope your models are somewhat like these
from django.db import models
from django.core.files.storage import FileSystemStorage
fs = FileSystemStorage(location="/var/www/yoursite/private/")
class SetOfFiles(models.Model):
name = models.CharField('set name'), max_length=225, null=False, blank=False)
class File(models.Model):
set = models.ForeignKey(SetOfFiles, verbose_name=_('set'))
file = models.FileField(storage=fs)
def save(self, *args, **kwargs):
if not self.id:
... unzip your file ...
... encrypt your file if necessary ...
super(File, self).save(*args, **kwargs)
Create admin.py in the related app customizing your admin to handle multiple insertion:
from django.contrib import admin
class FileInline(admin.TabularInline):
model = File
class SetOfFilesAdmin(admin.ModelAdmin):
list_display = ('name',)
inlines = [FileInline]
admin.site.register(SetOfFiles, SetOfFilesAdmin)
Here is also Unzip a .zip file uploaded with FileBrowser code but it could be a little bit complicated due to using FileBrowser app. You can unzip file just using the zipfile python module. Also you may use PyCrypto at AES encryption.
这篇关于django管理员上传文件的后处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!