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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With