Using django filer out of admin area (or an alternative library)

随声附和 提交于 2019-12-10 13:52:46

问题


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:


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>


来源:https://stackoverflow.com/questions/39798506/using-django-filer-out-of-admin-area-or-an-alternative-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!