5

Django filer 是一个很棒的文件管理工具,它可以检测重复文件并根据文件夹中的哈希值组织文件,具有用于管理文件和文件夹的出色 UI 并处理文件历史记录和权限。

我阅读了一些源代码并意识到它在代码和模板中广泛使用了 django 管理功能;有没有办法为已登录的编外人员使用这些功能?为他们提供在个人上传区域上传和管理自己的文件和文件夹的工具(无需重新发明轮子)?

如果没有简单的方法,有哪些替代方法,您建议以最少的代码更改来提供此类功能?

4

1 回答 1

1

根据这个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>
于 2017-04-12T22:57:00.547 回答