Skip to content

Instantly share code, notes, and snippets.

@juanriaza
Created May 16, 2012 18:10
Show Gist options
  • Save juanriaza/2712702 to your computer and use it in GitHub Desktop.
Save juanriaza/2712702 to your computer and use it in GitHub Desktop.
from django.conf import settings
from django.forms import forms
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render
class TestForm(forms.Form):
my_file = forms.FileField()
def handle_uploaded_file(file):
destination = open('%s/%s' % (settings.PROJECT_ROOT, file.name), 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
def home(request):
if request.method == 'POST':
form = TestForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['my_file'])
return HttpResponseRedirect(reverse('blog'))
else:
form = TestForm()
return render(request, "binnary/test.html", {'form': form})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment