Django upload file no model

Is it possible to upload a file to django using FileField in a form, but without a model? So far, I could only find examples with the model. I do not want to create a table for my database, I just want to upload a file. My form:

class csvUploadForm(forms.Form): csvFile = forms.FileField(label='Select a CSV file to upload.', help_text='help') 

Thanks,

Romny

+4
source share
2 answers

This is described in the Downloading Files section of the Django documentation. In short, use the UploadedFile instance specified in request.FILES .

0
source

An example reproduced by the Django Documentation .

 from django.http import HttpResponseRedirect from django.shortcuts import render_to_response def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) return HttpResponseRedirect('/success/url/') else: form = UploadFileForm() return render_to_response('upload.html', {'form': form}) def handle_uploaded_file(f): destination = open('some/file/name.txt', 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() 

You can replace 'some / file / name.txt' with another path where you want to save this file.

+6
source

All Articles