Django-file-formpreview is a project mainly written in Python, based on the MIT license.
django #7808 feature library
The package provides functionality written in bug#7808 <https://code.djangoproject.com/ticket/7808>
_
It provides two-step form submitting and actually based on django.contrib.formtools.FormPreview
,
but there are many updates, so it isn't subclassed
First, it validates form, uploads file and stores its name.
Then, it injects needing *_path fields to point correspondong files
In the second POST step it uses path given in the hidden *_path fields.
Additionally, it can render some fields from original form, if they are subclassed from PreviewField
as *_preview fields
Trying to submit another file path will fail, because file path depends on hash, which is calculated from:
# settings.py:
INSTALLED_APPS = (
...
'file_formpreview',
...
)
# forms.py:
from file_formpreview import FileFormPreview
class MyFileFormPreview(FileFormPreview):
...
[form_template = '...']
[preview_template = '...']
...
def done(self, request, cleaned_data):
"""
Does something with the cleaned_data and returns an
HttpResponseRedirect.
"""
...
# urls.py:
from my_forms import MyForm, MyFileFormPreview
url('^add-upload/$', FileFormPreview(MyForm)
OR views.py:
from my_forms import MyForm, MyFileFormPreview
def add_upload(request):
...
return MyFilePreviewForm(MyForm)(request)
it creates under it following structure: <settings.UPLOAD_DIR>/<YYYmmdd>/<hash>/<uploaded_file_name.ext>
<YYmmdd>
is somethind like "20110926"<hash>
is unique value calculated for every formDefault templates will behave like this:
Available in settings.py:
os.path.join(settings.MEDIA_ROOT, 'preview'))
(Note: it is autocleaned!)Available properties in your FileFormPreview subclass:
Available preview render configuration:
PreviewField
and rewrite render
methodIf you need something else, subclass FileFormPreview and implement process_preview
to get extended context in the preview template
ATTENTION: library doesn't check vulnerabilities like script uploads
Make sure you've setup webserver not to execute files from MEDIA_ROOT or give other upload_tmp parameter