Django filer是一个用于管理文件的强大工具,它可以根据文件夹中的哈希值检测重复项和组织文件,具有管理文件和文件夹的优秀UI,并处理文件历史记录和权限。
我阅读了一些源代码,并意识到它在代码和模板中广泛使用django管理功能;有没有办法为登录的非工作人员使用这些功能?为他们提供在个人上传区域上传和管理他们自己的文件和文件夹的工具(不重新发明轮子)?
如果没有简单的方法,有什么替代方案,你建议提供这样的功能,代码变化最小?
答案 0 :(得分:1)
根据this django-filer不应该在管理员之外工作,但是使用一些“胶水”我能够使上传工作在“普通”模板中。以下是我的一些代码:
# forms.py
class PollModelForm(forms.ModelForm):
uploaded_image = forms.ImageField(required=False)
class Meta:
model = Poll
fields = ['uploaded_image']
# views.py
# I used django-extra-views but you can use a normal cbv
class PollCreateView(LoginRequiredMixin, CreateWithInlinesView):
model = Poll
form_class = PollModelForm
template_name = 'polls/poll_form.html'
success_url = reverse_lazy('polls:poll-list')
inlines = [ChoiceInline]
# Powered by django-extra-views for the inlines so a bit different
@transaction.atomic
def forms_valid(self, form, inlines):
# It's more secure this way.
form.instance.user = self.request.user
uploaded_file = form.cleaned_data['uploaded_image']
image = Image.objects.create(
name=str(uploaded_file), is_public=True, file=uploaded_file,
description='Poll Image', owner=self.request.user
)
form.instance.image = image
log.info('Poll image uploaded'.format(**locals()))
return super(PollCreateView, self).forms_valid(form, inlines)
# HTML
<div class="form-group">
<input type="file" name="uploaded_image" id="id_uploaded_image">
<p class="help-block">Upload image here.</p>
</div>