python 在 Django 管理中上传多张图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/925305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading multiple images in Django admin
提问by Gary Chambers
I'm currently building a portfolio site for a client, and I'm having trouble with one small area. I want to be able to upload multiple images (varying number) inline for each portfolio item, and I can't see an obvious way to do it.
我目前正在为客户建立一个投资组合网站,但我在一个小区域中遇到了问题。我希望能够为每个投资组合项目内联上传多个图像(不同数量),但我看不到明显的方法。
The most user-friendly way I can see would be a file upload form with a JavaScript control that allows the user to add more fields as required. Has anybody had any experience with an issue like this? Indeed, are there any custom libraries out there that would solve my problem?
我能看到的最用户友好的方式是带有 JavaScript 控件的文件上传表单,允许用户根据需要添加更多字段。有没有人有过这样的问题的经验?确实,是否有任何自定义库可以解决我的问题?
I've had little call for modifying the admin tool before now, so I don't really know where to start.
我之前几乎没有要求修改管理工具,所以我真的不知道从哪里开始。
Thank you to anybody who can shed some light.
感谢任何可以照亮的人。
采纳答案by tghw
You can extend the Admin interface pretty easily using Javascript. There's a good articleon doing exactly what you want with a bit of jQuery magic.
您可以使用 Javascript 轻松扩展管理界面。有一篇很好的文章介绍了使用一些 jQuery 魔法来做你想做的事。
You would just have to throw all of his code into one Javascript file and then include the following in your admin.py:
您只需将他的所有代码都放入一个 Javascript 文件中,然后在您的 admin.py 中包含以下内容:
class Photo(admin.ModelAdmin):
class Media:
js = ('jquery.js', 'inlines.js',)
Looking at his source, you would also have to dynamically add the link to add more inlines using Javascript, but that's pretty easy to do:
查看他的来源,您还必须动态添加链接以使用 Javascript 添加更多内联,但这很容易做到:
$(document).ready(function(){
// Note the name passed in is the model's name, all lower case
$('div.last-related').after('<div><a class="add" href="#" onclick="return add_inline_form(\'photos\')">');
});
You probably need to do some styling to make it all look right, but that should get you started in the right direction.
您可能需要做一些样式以使其看起来正确,但这应该会让您朝着正确的方向开始。
Also, since you're in inline
land, check out the inline sort snippet.
此外,由于您在inline
陆地上,请查看内联排序代码段。
回答by miku
photologueis a feature-rich photo app for django. it e.g. lets you upload galleries as zip files (which in a sense means uploading multiple files at once), automatically creates thumbnails of different custom sizes and can apply effects to images. I used it once on one project and the integration wasn't too hard.
photologue是一个功能丰富的 django 照片应用程序。例如,它允许您将画廊上传为 zip 文件(从某种意义上说,这意味着一次上传多个文件),自动创建不同自定义大小的缩略图,并可以对图像应用效果。我曾经在一个项目中使用过它,集成起来并不太难。