使用django文件管理区域(或替代库) [英] Using django filer out of admin area (or an alternative library)

查看:114
本文介绍了使用django文件管理区域(或替代库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django文件管理器是管理文件的一个非常棒的工具,它可以根据文件夹中的哈希值来检测重复的文件,并且可以管理文件和文件夹,并处理文件历史和权限。

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.

我读了一些源代码,实现了它在代码和模板中广泛使用django管理功能;有没有办法使用这些功能登录的非员工?给他们上传和管理他们自己的文件和文件夹在他们的个人上传区域的工具(没有重新发明轮)?

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?

推荐答案

根据这个 django-filer不应该在管理员之外工作,而是用一些胶我能够通过正常模板进行上传。这是我的一些代码:

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>

这篇关于使用django文件管理区域(或替代库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆