Using django filer out of admin area (or an altern

2019-06-23 05:30发布

Django filer is an awesome tool for managing files, it detects duplicates and organizes files based on their hashes in folders, has great UI for managing files and folders and handles file history and permissions.

I read some of the source code and realized it extensively uses django admin features in code and in templates; is there any way to use these features for non-staff members that are logged in? To give them tools for uploading and managing their own files and folders in their personal upload area (without reinventing the wheel)?

If there isn't an easy way, what alternatives are there and you suggest to provide such functionality with minimum changes in code?

1条回答
神经病院院长
2楼-- · 2019-06-23 05:48

According to this django-filer is not supposed to work outside of the admin, but with some "glue" i was able to make the uploads work in a "normal" template. Here is some of my code:

    # 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>
查看更多
登录 后发表回答