1 | from django import forms
|
---|
2 | from django.core.files.uploadhandler import FileUploadHandler
|
---|
3 | from django.http import HttpResponse
|
---|
4 | from django.template import Template, RequestContext
|
---|
5 |
|
---|
6 |
|
---|
7 | class DummyUploadHandler(FileUploadHandler):
|
---|
8 | def receive_data_chunk(self, raw_data, start):
|
---|
9 | pass
|
---|
10 |
|
---|
11 | def file_complete(self, file_size):
|
---|
12 | pass
|
---|
13 |
|
---|
14 |
|
---|
15 | class UploadFileForm(forms.Form):
|
---|
16 | file = forms.FileField()
|
---|
17 |
|
---|
18 |
|
---|
19 | def upload_file(request):
|
---|
20 | # Raises AttributeError('You cannot alter upload handlers after the upload has been processed.')
|
---|
21 | request.upload_handlers.insert(0, DummyUploadHandler())
|
---|
22 |
|
---|
23 | if request.method == 'POST':
|
---|
24 | form = UploadFileForm(request.POST, request.FILES)
|
---|
25 | if form.is_valid():
|
---|
26 | template = Template('''
|
---|
27 | <!DOCTYPE html>
|
---|
28 | <title></title>
|
---|
29 | <h1>Done</h1>
|
---|
30 | ''')
|
---|
31 | context = RequestContext(request)
|
---|
32 | return HttpResponse(template.render(context))
|
---|
33 | else:
|
---|
34 | form = UploadFileForm()
|
---|
35 |
|
---|
36 | template = Template('''
|
---|
37 | <!DOCTYPE html>
|
---|
38 | <title></title>
|
---|
39 | <form action="." enctype="multipart/form-data" method="post">
|
---|
40 | {% csrf_token %}
|
---|
41 | {{ form }}
|
---|
42 | <input type="submit">
|
---|
43 | </form>
|
---|
44 | ''')
|
---|
45 | context = RequestContext(request, {'form': form})
|
---|
46 | return HttpResponse(template.render(context))
|
---|