【问题标题】:Using django filer out of admin area (or an alternative library)在管理区域(或替代库)之外使用 django 文件管理器
【发布时间】:2016-09-30 19:08:00
【问题描述】:

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

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

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

【问题讨论】:

    标签: django django-filer


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2016-12-07
      • 2016-09-10
      • 1970-01-01
      • 2021-04-06
      • 2017-08-02
      • 1970-01-01
      相关资源
      最近更新 更多