0

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)
    )
Tomas S
  • 1
  • 4
  • It's an irrelevant detail at this point, but your example uses inconsistent replacement for space character. `main street` becomes `main_street` and `flat 1` becomes `flat1`. – nothingisnecessary Jul 22 '15 at 17:15
  • Can you please also show the code that saves the file to the "specific location" you refer to? If you do that I can customize the answer to fit your code. – nothingisnecessary Jul 22 '15 at 17:17
  • pleas see the update – Tomas S Jul 24 '15 at 13:27
  • Might be the answer from this question will help: http://stackoverflow.com/questions/5135556/dynamic-file-path-in-django – gaurav_kamble Jul 27 '15 at 15:13
  • Thank you for the link, i was looking at it few times but cannot get it to work, and looks like it is using id rather then template url, which i would like to use . – Tomas S Jul 28 '15 at 11:55

1 Answers1

0

You can use request.get_full_path() to get the path for the requested page.

See this related question for another example, and read the official docs here.

Community
  • 1
  • 1
nothingisnecessary
  • 6,099
  • 36
  • 60
  • Hi, thank you so much for you response, not sure how else to explain : cureewnt setup was in models.py class Document(models.Model): docfile = models.FileField(upload_to='documents/%Y/%m/%d') but i want it to save to documents/main_street/houseį/flat1 for this particular template, obviously for other flats which are other 20 i need for the files to be saved in their own directories as per the paged opened at the time. hope makes sense. thanks again – Tomas S Jul 23 '15 at 10:06