python - Django 1.8 uploading files for a specific location -
spent day searching , hope can give me answer.
can please advise on how make django create file directory when uploading files according page opened.
for example, need create web page flats each flat have ability files uploaded.
i know how upload specific location, there way django read page opened , create directory page's name, let's say:
www.comunity.com/main street/house5/flat 1
i directory created the
main_street/house5/flat1
and image stored there.
hope makes sense.
currently models.py
# -*- coding: utf-8 -*- django.db import models class document(models.model): docfile = models.filefield(upload_to='documents/%y/%m/%d')
and forms.py
class documentform(forms.form): docfile = forms.filefield( label='pasirinkite dokumenta' )
views.py
from newsletter.models import document 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 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() # empty, unbound form # load documents list page documents = document.objects.all() # render list page documents , form return render_to_response( 'main_street_6_flat_1.html', {'documents': documents, 'form': form}, context_instance=requestcontext(request) )
you can use request.get_full_path()
path requested page.
see this related question example, , read official docs here.
Comments
Post a Comment