Spent all day searching and hope someone can give me an answer.
Can someone please advise on how to make Django create file directory when uploading files according to the page opened.
For example, I need to create web page for flats where each flat will have ability for the files to be uploaded.
I know how to upload to a specific location, but is there a way for Django to read the page that is opened and create a directory with that page's name, let's say:
www.comunity.com/main street/house5/flat 1
I would like the directory to be created with the
main_street/house5/flat1
and the image to be stored there.
Hope this makes sense.
currently models.py
# -*- coding: utf-8 -*-
from django.db import models
class Document(models.Model):
docfile = models.FileField(upload_to='documents/%Y/%m/%d')
And the forms.py
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Pasirinkite dokumenta'
)
Views.py
from newsletter.models import Document
from newsletter.forms import DocumentForm
def Main_street_6_flat_1(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'])
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('Main_street_6_flat_1'))
if request.method != 'POST':
raise Http404
docId = request.POST.get('docfile', None)
docToDel = get_object_or_404(Document, pk = docId)
docToDel.docfile.delete()
docToDel.delete()
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render_to_response(
'Main_street_6_flat_1.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)