Generally speaking when you are trying to 'just get a working example' it is best to 'just start writing code'. There is no code here to help you with, so it makes answering the question a lot more work for us.
If you want to grab a file, you need something like this in an html file somewhere:
That will give you the browse button, an upload button to start the action (submit the form) and note the enctype so Django knows to give you request.FILES
In a view somewhere you can access the file with
def myview(request):
request.FILES['myfile'] # this is my file
for key, file in request.FILES.items():
path = file.name
dest = open(path, 'w')
if file.multiple_chunks:
for c in file.chunks():
dest.write(c)
else:
dest.write(file.read())
dest.close()
I must say I find the documentation at django confusing. Also for the simplest example why are forms being mentioned? The example I got to work in the views.py is :-
for key, file in request.FILES.items():
path = file.name
dest = open(path, 'w')
if file.multiple_chunks:
for c in file.chunks():
dest.write(c)
else:
dest.write(file.read())
dest.close()
The html file looks like the code below, though this example only uploads one file and the code to save the files handles many :-
These examples are not my code, they have been optained from two other examples I found. I am a relative beginner to django so it is very likely I am missing some key point.
I also had the similar requirement. Most of the examples on net are asking to create models and create forms which I did not wanna use. Here is my final code.
if request.method == 'POST':
file1 = request.FILES['file']
contentOfFile = file1.read()
if file1:
return render(request, 'blogapp/Statistics.html', {'file': file1, 'contentOfFile': contentOfFile})
You can call this handle_uploaded_file function from your view with the uploaded file object. This will save the file with a unique name (prefixed with filename of the original uploaded file) in filesystem and return the full path of saved file. You can save the path in database, and do something with the file later.
class UploadView(View):
@csrf_exempt
def dispatch(self, *args, **kwargs):
return super(UploadView, self).dispatch(*args, **kwargs)
def post(self, request, *args, **kwargs):
"""A POST request. Validate the form and then handle the upload
based ont the POSTed data. Does not handle extra parameters yet.
"""
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_upload(request.FILES['qqfile'], form.cleaned_data)
return make_response(content=json.dumps({ 'success': True }))
else:
return make_response(status=400,
content=json.dumps({
'success': False,
'error': '%s' % repr(form.errors)
}))
def delete(self, request, *args, **kwargs):
"""A DELETE request. If found, deletes a file with the corresponding
UUID from the server's filesystem.
"""
qquuid = kwargs.get('qquuid', '')
if qquuid:
try:
handle_deleted_file(qquuid)
return make_response(content=json.dumps({ 'success': True }))
except Exception, e:
return make_response(status=400,
content=json.dumps({
'success': False,
'error': '%s' % repr(e)
}))
return make_response(status=404,
content=json.dumps({
'success': False,
'error': 'File not present'
}))
forms.py
class UploadFileForm(forms.Form):
""" This form represents a basic request from Fine Uploader.
The required fields will **always** be sent, the other fields are optional
based on your setup.
Edit this if you want to add custom parameters in the body of the POST
request.
"""
qqfile = forms.FileField()
qquuid = forms.CharField()
qqfilename = forms.CharField()
qqpartindex = forms.IntegerField(required=False)
qqchunksize = forms.IntegerField(required=False)
qqpartbyteoffset = forms.IntegerField(required=False)
qqtotalfilesize = forms.IntegerField(required=False)
qqtotalparts = forms.IntegerField(required=False)
It's very elegant and most important of all, it provides featured js lib. Template is not included in server-examples, but you can find demo on its website. Fine Uploader: http://fineuploader.com/demos.html
django-fine-uploader
views.py
UploadView dispatches post and delete request to respective handlers.
class UploadView(View):
@csrf_exempt
def dispatch(self, *args, **kwargs):
return super(UploadView, self).dispatch(*args, **kwargs)
def post(self, request, *args, **kwargs):
"""A POST request. Validate the form and then handle the upload
based ont the POSTed data. Does not handle extra parameters yet.
"""
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_upload(request.FILES['qqfile'], form.cleaned_data)
return make_response(content=json.dumps({ 'success': True }))
else:
return make_response(status=400,
content=json.dumps({
'success': False,
'error': '%s' % repr(form.errors)
}))
def delete(self, request, *args, **kwargs):
"""A DELETE request. If found, deletes a file with the corresponding
UUID from the server's filesystem.
"""
qquuid = kwargs.get('qquuid', '')
if qquuid:
try:
handle_deleted_file(qquuid)
return make_response(content=json.dumps({ 'success': True }))
except Exception, e:
return make_response(status=400,
content=json.dumps({
'success': False,
'error': '%s' % repr(e)
}))
return make_response(status=404,
content=json.dumps({
'success': False,
'error': 'File not present'
}))
forms.py
class UploadFileForm(forms.Form):
""" This form represents a basic request from Fine Uploader.
The required fields will **always** be sent, the other fields are optional
based on your setup.
Edit this if you want to add custom parameters in the body of the POST
request.
"""
qqfile = forms.FileField()
qquuid = forms.CharField()
qqfilename = forms.CharField()
qqpartindex = forms.IntegerField(required=False)
qqchunksize = forms.IntegerField(required=False)
qqpartbyteoffset = forms.IntegerField(required=False)
qqtotalfilesize = forms.IntegerField(required=False)
qqtotalparts = forms.IntegerField(required=False)
# models
class Document(models.Model):
docfile = models.FileField(upload_to='documents/Temp/%Y/%m/%d')
def doc_name(self):
return self.docfile.name.split('/')[-1] # only the name, not full path
# admin
from myapp.models import Document
class DocumentAdmin(admin.ModelAdmin):
list_display = ('doc_name',)
admin.site.register(Document, DocumentAdmin)
I faced the similar problem, and solved by django admin site.
# models
class Document(models.Model):
docfile = models.FileField(upload_to='documents/Temp/%Y/%m/%d')
def doc_name(self):
return self.docfile.name.split('/')[-1] # only the name, not full path
# admin
from myapp.models import Document
class DocumentAdmin(admin.ModelAdmin):
list_display = ('doc_name',)
admin.site.register(Document, DocumentAdmin)
# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from myproject.myapp.models import Document
from myproject.myapp.forms import DocumentForm
def list(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('myapp.views.list'))
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(
'myapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
Phew, Django documentation really does not have good example about this. I spent over 2 hours to dig up all the pieces to understand how this works. With that knowledge I implemented a project that makes possible to upload files and show them as list. To download source for the project, visit https://github.com/axelpale/minimal-django-file-upload-example or clone it:
Update 2013-01-30: The source at GitHub has also implementation for Django 1.4 in addition to 1.3. Even though there is few changes the following tutorial is also useful for 1.4.
Update 2013-05-10: Implementation for Django 1.5 at GitHub. Minor changes in redirection in urls.py and usage of url template tag in list.html. Thanks to hubert3 for the effort.
Update 2013-12-07: Django 1.6 supported at GitHub. One import changed in myapp/urls.py. Thanks goes to Arthedian.
Update 2015-03-17: Django 1.7 supported at GitHub, thanks to aronysidoro.
Update 2015-09-04: Django 1.8 supported at GitHub, thanks to nerogit.
Update 2016-07-03: Django 1.9 supported at GitHub, thanks to daavve and nerogit
Project tree
A basic Django 1.3 project with single app and media/ directory for uploads.
To upload and serve files, you need to specify where Django stores uploaded files and from what URL Django serves them. MEDIA_ROOT and MEDIA_URL are in settings.py by default but they are empty. See the first lines in Django Managing Files for details. Remember also set the database and add myapp to INSTALLED_APPS
Next you need a model with a FileField. This particular field stores files e.g. to media/documents/2011/12/24/ based on current date and MEDIA_ROOT. See FileField reference.
# -*- coding: utf-8 -*-
from django.db import models
class Document(models.Model):
docfile = models.FileField(upload_to='documents/%Y/%m/%d')
3. Form: myproject/myapp/forms.py
To handle upload nicely, you need a form. This form has only one field but that is enough. See Form FileField reference for details.
# -*- coding: utf-8 -*-
from django import forms
class DocumentForm(forms.Form):
docfile = forms.FileField(
label='Select a file',
help_text='max. 42 megabytes'
)
4. View: myproject/myapp/views.py
A view where all the magic happens. Pay attention how request.FILES are handled. For me, it was really hard to spot the fact that request.FILES['docfile'] can be saved to models.FileField just like that. The model's save() handles the storing of the file to the filesystem automatically.
# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from myproject.myapp.models import Document
from myproject.myapp.forms import DocumentForm
def list(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('myapp.views.list'))
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(
'myapp/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
5. Project URLs: myproject/urls.py
Django does not serve MEDIA_ROOT by default. That would be dangerous in production environment. But in development stage, we could cut short. Pay attention to the last line. That line enables Django to serve files from MEDIA_URL. This works only in developement stage.
The last part: template for the list and the upload form below it. The form must have enctype-attribute set to "multipart/form-data" and method set to "post" to make upload to Django possible. See File Uploads documentation for details.
The FileField has many attributes that can be used in templates. E.g. {{ document.docfile.url }} and {{ document.docfile.name }} as in the template. See more about these in Using files in models article and The File object documentation.
Finally, everything is ready. On default Django developement environment the list of uploaded documents can be seen at localhost:8000/list/. Today the files are uploaded to /path/to/myproject/media/documents/2011/12/17/ and can be opened from the list.
I hope this answer will help someone as much as it would have helped me.
The last part: template for the list and the upload form below it. The form must have enctype-attribute set to "multipart/form-data" and method set to "post" to make upload to Django possible. See File Uploads documentation for details.
The FileField has many attributes that can be used in templates. E.g. {{ document.docfile.url }} and {{ document.docfile.name }} as in the template. See more about these in Using files in models article and The File object documentation.
8. Initialize
Just run syncdb and runserver.
Results
Finally, everything is ready. On default Django developement environment the list of uploaded documents can be seen at localhost:8000/list/. Today the files are uploaded to /path/to/myproject/media/documents/2011/12/17/ and can be opened from the list.
I hope this answer will help someone as much as it would have helped me.
发布评论
评论(10)
演示
请参阅github repo,适用于 Django 3
一个最小的 Django 文件上传示例
1. 创建一个 django 项目
运行 startproject::
现在创建了一个文件夹(sample)。
2. 创建一个应用程序
创建一个应用程序::
现在创建一个包含这些文件的文件夹(
uploader
)::3. 更新 settings.py
在
sample/settings.py
添加'uploader'
到INSTALLED_APPS
并添加MEDIA_ROOT
和MEDIA_URL
,即::更新 urls.py
4.
sample/urls.py
add::5.更新models.py
更新
uploader/models.py
::6.更新views.py
更新
uploader/views。 py
::7.创建模板
创建文件夹sample/uploader/templates/uploader
创建文件upload_form.html 即
sample/uploader/templates/uploader/upload_form.html
::8.同步数据库
同步数据库和runserver::
访问http://localhost:8000/
Demo
See the github repo, works with Django 3
A minimal Django file upload example
1. Create a django project
Run startproject::
now a folder(sample) is created.
2. create an app
Create an app::
Now a folder(
uploader
) with these files are created::3. Update settings.py
On
sample/settings.py
add'uploader'
toINSTALLED_APPS
and addMEDIA_ROOT
andMEDIA_URL
, ie::4. Update urls.py
in
sample/urls.py
add::5. Update models.py
update
uploader/models.py
::6. Update views.py
update
uploader/views.py
::7. create templates
Create a folder sample/uploader/templates/uploader
Create a file upload_form.html ie
sample/uploader/templates/uploader/upload_form.html
::8. Syncronize database
Syncronize database and runserver::
visit http://localhost:8000/
一般来说,当您尝试“只是获得一个工作示例”时,最好“开始编写代码”。这里没有代码可以帮助您,因此这使得回答这个问题对我们来说更加困难。
如果你想抓取一个文件,你需要在一个 html 文件中的某个地方有这样的东西:
这将为你提供浏览按钮,一个上传按钮来启动操作(提交表单)并记下 enctype,以便 Django 知道给你< code>request.FILES
在某个视图中,您可以使用以下命令访问该文件
文件上传文档
我建议您仔细阅读该页面,然后开始编写代码 - 然后在它不起作用时返回示例和堆栈跟踪。
Generally speaking when you are trying to 'just get a working example' it is best to 'just start writing code'. There is no code here to help you with, so it makes answering the question a lot more work for us.
If you want to grab a file, you need something like this in an html file somewhere:
That will give you the browse button, an upload button to start the action (submit the form) and note the enctype so Django knows to give you
request.FILES
In a view somewhere you can access the file with
There is a huge amount of information in the file upload docs
I recommend you read the page thoroughly and just start writing code - then come back with examples and stack traces when it doesn't work.
我必须说我发现 django 的文档令人困惑。
还是举个最简单的例子,为什么要提到表格?
我在views.py中工作的例子是:
-html文件看起来像下面的代码,尽管这个例子只上传一个文件,而保存文件的代码处理许多文件:-
这些例子不是我的代码,他们有是从我发现的另外两个例子中获得的。
我是 django 的相对初学者,所以很可能我错过了一些关键点。
I must say I find the documentation at django confusing.
Also for the simplest example why are forms being mentioned?
The example I got to work in the views.py is :-
The html file looks like the code below, though this example only uploads one file and the code to save the files handles many :-
These examples are not my code, they have been optained from two other examples I found.
I am a relative beginner to django so it is very likely I am missing some key point.
我也有类似的要求。网上的大多数示例都要求创建模型并创建我不想使用的表单。这是我的最终代码。
在要上传的 HTML 中我写道:
以下是显示文件内容的 HTML:
I also had the similar requirement. Most of the examples on net are asking to create models and create forms which I did not wanna use. Here is my final code.
And in HTML to upload I wrote:
Following is the HTML which displays content of file:
扩展 Henry 的示例:
您可以调用此 < code>handle_uploaded_file 函数从您的视图中使用上传的文件对象。这将以唯一的名称(以原始上传文件的文件名为前缀)保存文件在文件系统中,并返回保存文件的完整路径。您可以将路径保存在数据库中,并稍后对该文件执行某些操作。
Extending on Henry's example:
You can call this
handle_uploaded_file
function from your view with the uploaded file object. This will save the file with a unique name (prefixed with filename of the original uploaded file) in filesystem and return the full path of saved file. You can save the path in database, and do something with the file later.这里它可能对你有帮助:
在 models.py 中创建一个文件字段
用于上传文件(在 admin.py 中):
并在模板中使用该字段。
Here it may helps you:
create a file field in your models.py
For uploading the file(in your admin.py):
and use that field in your template also.
您可以参考 Fine Uploader 中的服务器示例,它有 django 版本。
https://github.com/FineUploader/server-examples /tree/master/python/django-fine-uploader
它非常优雅,最重要的是,它提供了特色的 js 库。服务器示例中不包含模板,但您可以在其网站上找到演示。
精细上传器: http://fineuploader.com/demos.html
django-fine-uploader
视图.py
UploadView 将发布和删除请求分派给各自的处理程序。
forms.py
You can refer to server examples in Fine Uploader, which has django version.
https://github.com/FineUploader/server-examples/tree/master/python/django-fine-uploader
It's very elegant and most important of all, it provides featured js lib. Template is not included in server-examples, but you can find demo on its website.
Fine Uploader: http://fineuploader.com/demos.html
django-fine-uploader
views.py
UploadView dispatches post and delete request to respective handlers.
forms.py
不确定这种方法是否有任何缺点,但在views.py中甚至更小:
Not sure if there any disadvantages to this approach but even more minimal, in views.py:
我遇到了类似的问题,并通过 django 管理站点解决了。
I faced the similar problem, and solved by django admin site.
唷,Django 文档确实没有关于此的好的示例。我花了两个多小时来挖掘所有内容以了解其工作原理。有了这些知识,我实现了一个项目,可以上传文件并将其显示为列表。要下载该项目的源代码,请访问 https://github.com/axelpale/minimal -django-file-upload-example 或克隆它:
更新 2013-01-30: GitHub 上的源代码除了 Django 1.3 之外还实现了 Django 1.4。尽管有一些变化,以下教程对于 1.4 也很有用。
2013-05-10 更新: GitHub 上的 Django 1.5 实现。 urls.py 中的重定向和 list.html 中 url 模板标记的使用进行了细微更改。感谢 hubert3 的努力。
2013-12-07 更新: GitHub 支持 Django 1.6。 myapp/urls.py 中的一项导入发生了变化。感谢 Arthedian。
2015-03-17 更新: GitHub 支持 Django 1.7,感谢 aronysidoro。
更新 2015-09-04: GitHub 支持 Django 1.8,感谢 nerogit。
2016-07-03 更新: GitHub 支持 Django 1.9,感谢 daavve 和 < a href="https://github.com/nerogit" rel="noreferrer">nerogit
项目树
一个基本的 Django 1.3 项目,具有单个应用程序和用于上传的 media/ 目录。
1. 设置:myproject/settings.py
要上传和提供文件,您需要指定 Django 存储上传文件的位置以及 Django 从哪个 URL 提供这些文件。 MEDIA_ROOT 和 MEDIA_URL 默认位于 settings.py 中,但它们是空的。有关详细信息,请参阅 Django 管理文件 中的第一行。还记得设置数据库并将 myapp 添加到 INSTALLED_APPS
2. 模型:myproject/myapp/models.py
接下来,您需要一个带有 FileField 的模型。该特定字段根据当前日期和 MEDIA_ROOT 将文件存储到例如 media/documents/2011/12/24/。请参阅FileField 参考。
3. 表单:myproject/myapp/forms.py
为了更好地处理上传,您需要一个表单。该表格只有一个字段,但这已经足够了。有关详细信息,请参阅表单文件字段参考。
4. 视图:myproject/myapp/views.py
所有魔法都发生在这个视图中。注意 request.FILES 是如何处理的。对我来说,很难发现
request.FILES['docfile']
可以像这样保存到 models.FileField 中。模型的 save() 自动处理文件到文件系统的存储。5. 项目 URL:myproject/urls.py
Django 默认不提供 MEDIA_ROOT 服务。这在生产环境中会很危险。但在开发阶段,我们可以缩短时间。注意最后一行。该行使 Django 能够从 MEDIA_URL 提供文件。这仅在开发阶段有效。
请参阅 django.conf.urls.static .静态参考了解详细信息。另请参阅有关服务的讨论媒体文件。
6. 应用程序 URL:myproject/myapp/urls.py
要使视图可访问,您必须为其指定 url。这里没什么特别的。
7. 模板:myproject/myapp/templates/myapp/list.html
最后一部分:列表及其下面的上传表单的模板。表单必须将 enctype-attribute 设置为“multipart/form-data”并将方法设置为“post”才能上传到 Django。有关详细信息,请参阅文件上传文档。
FileField 有许多可在模板中使用的属性。例如模板中的 {{ document.docfile.url }} 和 {{ document.docfile.name }} 。有关这些内容的更多信息,请参阅在模型中使用文件一文 和 File 对象文档。
8.初始化
只需运行syncdb和runserver。
结果
终于一切准备就绪。在默认的 Django 开发环境中,可以在 localhost:8000/list/ 中看到上传的文档列表。今天,文件已上传到 /path/to/myproject/media/documents/2011/12/17/ 并可以从列表中打开。
我希望这个答案能帮助别人,就像它对我有帮助一样。
Phew, Django documentation really does not have good example about this. I spent over 2 hours to dig up all the pieces to understand how this works. With that knowledge I implemented a project that makes possible to upload files and show them as list. To download source for the project, visit https://github.com/axelpale/minimal-django-file-upload-example or clone it:
Update 2013-01-30: The source at GitHub has also implementation for Django 1.4 in addition to 1.3. Even though there is few changes the following tutorial is also useful for 1.4.
Update 2013-05-10: Implementation for Django 1.5 at GitHub. Minor changes in redirection in urls.py and usage of url template tag in list.html. Thanks to hubert3 for the effort.
Update 2013-12-07: Django 1.6 supported at GitHub. One import changed in myapp/urls.py. Thanks goes to Arthedian.
Update 2015-03-17: Django 1.7 supported at GitHub, thanks to aronysidoro.
Update 2015-09-04: Django 1.8 supported at GitHub, thanks to nerogit.
Update 2016-07-03: Django 1.9 supported at GitHub, thanks to daavve and nerogit
Project tree
A basic Django 1.3 project with single app and media/ directory for uploads.
1. Settings: myproject/settings.py
To upload and serve files, you need to specify where Django stores uploaded files and from what URL Django serves them. MEDIA_ROOT and MEDIA_URL are in settings.py by default but they are empty. See the first lines in Django Managing Files for details. Remember also set the database and add myapp to INSTALLED_APPS
2. Model: myproject/myapp/models.py
Next you need a model with a FileField. This particular field stores files e.g. to media/documents/2011/12/24/ based on current date and MEDIA_ROOT. See FileField reference.
3. Form: myproject/myapp/forms.py
To handle upload nicely, you need a form. This form has only one field but that is enough. See Form FileField reference for details.
4. View: myproject/myapp/views.py
A view where all the magic happens. Pay attention how
request.FILES
are handled. For me, it was really hard to spot the fact thatrequest.FILES['docfile']
can be saved to models.FileField just like that. The model's save() handles the storing of the file to the filesystem automatically.5. Project URLs: myproject/urls.py
Django does not serve MEDIA_ROOT by default. That would be dangerous in production environment. But in development stage, we could cut short. Pay attention to the last line. That line enables Django to serve files from MEDIA_URL. This works only in developement stage.
See django.conf.urls.static.static reference for details. See also this discussion about serving media files.
6. App URLs: myproject/myapp/urls.py
To make the view accessible, you must specify urls for it. Nothing special here.
7. Template: myproject/myapp/templates/myapp/list.html
The last part: template for the list and the upload form below it. The form must have enctype-attribute set to "multipart/form-data" and method set to "post" to make upload to Django possible. See File Uploads documentation for details.
The FileField has many attributes that can be used in templates. E.g. {{ document.docfile.url }} and {{ document.docfile.name }} as in the template. See more about these in Using files in models article and The File object documentation.
8. Initialize
Just run syncdb and runserver.
Results
Finally, everything is ready. On default Django developement environment the list of uploaded documents can be seen at
localhost:8000/list/
. Today the files are uploaded to /path/to/myproject/media/documents/2011/12/17/ and can be opened from the list.I hope this answer will help someone as much as it would have helped me.