Django 1.8上传特定位置的文件 [英] Django 1.8 uploading files for a specific location
问题描述
有人可以告诉您如何使Django在根据页面上传文件时创建文件目录打开。
例如,我需要为单位创建网页,每个单位将有能力上传文件。
我知道如何上传到特定的位置,但Django是否可以阅读打开的页面,并创建一个具有该页面名称的目录,让我们说:
www.comunity.com/main street / house5 / flat 1
/ pre>
我想使用
main_street / house5 / flat1
和要存储的图像。
希望这是有道理的。
目前models.py
# - * - 编码:utf-8 - * -
从django.db导入模型
class Document(models.Model) :
docfile = models.FileField(upload_to ='documents /%Y /%m /%d')
和form.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.method =='POST':
form = DocumentForm(request.POST,request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES ['docfile'])
newdoc.save()
#重定向到POST
返回HttpResponseRedirect(reverse('Main_street_6_flat_1'))
如果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()#一个空的,未绑定的表单
#加载列表页面的文档
documents = Document.objects.all( )
#具有文档和表单
的渲染列表页面返回render_to_response(
'Main_street_6_flat_1.html',
{'documents':documents,'form' :form},
context_instance = RequestContext(request)
)
解决方案您可以使用
request.get_full_path()
获取请求页面的路径。
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) )
解决方案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.
这篇关于Django 1.8上传特定位置的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!