As a newbie to Django, I am having difficulty making an upload app in Django 1.3. I could not find any up-to-date example/snippets. May someone post a minimal but complete (Model, View, Template) example code to do so?
相关问题
- Django __str__ returned non-string (type NoneType)
- What is the best way to do a search in a large fil
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- What is the correct way to declare and use a FILE
- Django/Heroku: FATAL: too many connections for rol
- Making new files automatically executable?
- Django is sooo slow? errno 32 broken pipe? dcramer
- How to serialize data into indented json [duplicat
Not sure if there any disadvantages to this approach but even more minimal, in views.py:
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.
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.
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:
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
Demo
Update of Akseli Palén's answer. see the github repo, works with Django 2
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.apps.UploaderConfig'
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 templates in folder uploader, then create a file home.html, ie
sample/uploader/templates/home.html
::8. Syncronize database
Syncronize database and runserver::
visit http://localhost.com:8000