0

I have a django app which has a file upload feature. I am currently using forms in an HTML template, instead of using django forms. my current code is as follows:

views.py

def new(request):
    file_path = 'c:\\Users\\Me\\Desktop\\'+request.POST['myFile']
    newFile = DjangoFile(file(file_path))
    obj = MyObj(myFile=newFile)
    obj.save()

according to this code, i had to specify the base file path in order to read it and save it in the MEDIA_ROOT directory. Is there any way I can read the path of the file being uploaded, instead of providing a hard coded base path?

My html template has the following code:

<p>My File: <input type="file" name="myFile" class="form-control input-md-4" id="myFile" /></p><br />
Gaurav Sood
  • 680
  • 4
  • 17
  • 38
  • This snippet makes no sense. You are setting `file_path` but are not using it anywhere after it. I suggest you get back to the docs about file uploading in Django. – Paulo Scardine Jan 29 '15 at 11:54
  • my bad. using file_path now – Gaurav Sood Jan 29 '15 at 11:55
  • Again, I suggest [reading the docs](https://docs.djangoproject.com/en/1.7/topics/http/file-uploads/). You can get a proper `File` object from `request.FILES` in Django but there are a lot of other little details you must address, including some security concerns. As currently stated your question shows a lack of research effort that is likely to get it closed as "too broad". – Paulo Scardine Jan 29 '15 at 12:00

0 Answers0