mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #17016 - Added examples for file uploads in views.
Thanks Tim Saylor for the draft patch and Aymeric Augustin and Claude Paroz for feedback.
This commit is contained in:
		
							
								
								
									
										1
									
								
								AUTHORS
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								AUTHORS
									
									
									
									
									
								
							| @@ -467,6 +467,7 @@ answer newbie questions, and generally made Django that much better: | ||||
|     Vinay Sajip <vinay_sajip@yahoo.co.uk> | ||||
|     Bartolome Sanchez Salado <i42sasab@uco.es> | ||||
|     Kadesarin Sanjek | ||||
|     Tim Saylor <tim.saylor@gmail.com> | ||||
|     Massimo Scamarcia <massimo.scamarcia@gmail.com> | ||||
|     Paulo Scardine <paulo@scardine.com.br> | ||||
|     David Schein | ||||
|   | ||||
| @@ -178,6 +178,50 @@ Three settings control Django's file upload behavior: | ||||
|     Which means "try to upload to memory first, then fall back to temporary | ||||
|     files." | ||||
|  | ||||
| Handling uploaded files with a model | ||||
| ------------------------------------ | ||||
|  | ||||
| If you're saving a file on a :class:`~django.db.models.Model` with a | ||||
| :class:`~django.db.models.FileField`, using a :class:`~django.forms.ModelForm` | ||||
| makes this process much easier. The file object will be saved when calling | ||||
| ``form.save()``:: | ||||
|  | ||||
|     from django.http import HttpResponseRedirect | ||||
|     from django.shortcuts import render | ||||
|     from .forms import ModelFormWithFileField | ||||
|  | ||||
|     def upload_file(request): | ||||
|         if request.method == 'POST': | ||||
|             form = ModelFormWithFileField(request.POST, request.FILES) | ||||
|             if form.is_valid(): | ||||
|                 # file is saved | ||||
|                 form.save() | ||||
|                 return HttpResponseRedirect('/success/url/') | ||||
|         else: | ||||
|             form = ModelFormWithFileField() | ||||
|         return render('upload.html', {'form': form}) | ||||
|  | ||||
| If you are constructing an object manually, you can simply assign the file | ||||
| object from :attr:`request.FILES <django.http.HttpRequest.FILES>` to the file | ||||
| field in the model:: | ||||
|  | ||||
|     from django.http import HttpResponseRedirect | ||||
|     from django.shortcuts import render | ||||
|     from .forms import UploadFileForm | ||||
|     from .models import ModelWithFileField | ||||
|  | ||||
|     def upload_file(request): | ||||
|         if request.method == 'POST': | ||||
|             form = UploadFileForm(request.POST, request.FILES) | ||||
|             if form.is_valid(): | ||||
|                 instance = ModelWithFileField(file_field=request.FILES['file']) | ||||
|                 instance.save() | ||||
|                 return HttpResponseRedirect('/success/url/') | ||||
|         else: | ||||
|             form = UploadFileForm() | ||||
|         return render('upload.html', {'form': form}) | ||||
|  | ||||
|  | ||||
| ``UploadedFile`` objects | ||||
| ======================== | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user