I have this snippet in one of the functions in views.py:
# Handle file upload
if request.method == 'POST':
form = BookForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Book()
request.FILES['docfile'].open('w+')
text = request.FILES['docfile'].read()
text = text.replace('\n', '<br>')
request.FILES['docfile'].seek(0)
request.FILES['docfile'].write(text)
newdoc.document = request.FILES['docfile']
newdoc.owner = request.user
newdoc.title = request.POST['title']
newdoc.save()
This works on my computer, allowing me to upload a text file, replace all the newlines with html breaks, and save it.
It works on PythonAnywhere without any complaints at all. But for some reason it forgets to do the string replace. But the file saves fine. I guess I must not have write access to the tmp uploaded file or something.
Now, I have no clear understanding of what I'm doing and I'm sure there's a cleaner, better practise way to handle files - so I welcome any advice or better examples in that area.
But most importantly, how can I get this working in a hurry?