Django-Tastypie how to process files upload and download
Django-Tastypie is a powerful tool for building a RESTFUL API. It comes with the function of processing file uploading and downloading.This article will introduce how to use Django-Tastypie to process files upload and download, and provide related programming code and configuration.
1. Installation and configuration Django-tastypie
First, make sure that Django-Tastypie has been installed into your project.You can install it by running the following command:
pip install django-tastypie
Next, add `tastypie` to the` settings.py` file of your Django project to the `Installed_apps`:
python
INSTALLED_APPS = [
...
'tastypie',
]
2. Create a resource
Create a resource in your Django project to process files upload and download.Suppose we create a resource called `FileReresource`:
python
from django.contrib.auth.models import User
from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.authorization import Authorization
from tastypie.utils import trailing_slash
from django.core.files.base import ContentFile
class FileResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = File.objects.all()
resource_name = 'file'
authorization = Authorization()
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/(?P<pk>\w+)/download%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('download'), name="api_download"),
]
def download(self, request, **kwargs):
file_obj = self.obj_get(request, **kwargs)
file_content = file_obj.file.read()
file_content_type = mimetypes.guess_type(file_obj.file.name)[0]
response = HttpResponse(file_content, content_type=file_content_type)
response['Content-Disposition'] = 'attachment; filename=%s' % file_obj.file.name
return response
In the above code, first of all, we introduced some of the required packages and modules.Then, we define a resource called `FileReresource`, which contains an outer key field` user`.Note that we also used the `ModelReSource` class as the base class.
In the `Meta` class, we designate the relevant model class` file` and resource name `file`, and use the` Authorization` class to set access permissions.
In the `Prepend_URLS` method, we define a URL mode for processing file download.This url mode will match the following URL format: `/API/V1/File/{file_id}/download/`.
In the `DOWNLOAD` method, we first obtain the file object and read the content of the file.Then, we guess the MIME type of the file through the `Mimetypes` module.Finally, we create a response with a file with file content, and set the corresponding `Content-Disposition.
3. Configure URL route
In the `urls.py` file of your Django project, add the URL routing of the` Tastypie` to the project of the project:
python
from django.conf.urls import include, url
from tastypie.api import Api
v1_api = Api(api_name='v1')
v1_api.register(FileResource())
urlpatterns = [
...
url(r'^api/', include(v1_api.urls)),
]
4. Process file upload
To be uploaded by files, we need to add a `FileField` field to the resources, and set the` allthod_Methods` in the `meta` class to ['get', 'post', 'port', 'delete'].
python
from tastypie.fields import FileField
class FileResource(ModelResource):
file = FileField(attribute='file', null=True, blank=True)
user = fields.ForeignKey(UserResource, 'user')
class Meta:
allowed_methods = ['get', 'post', 'put', 'delete']
queryset = File.objects.all()
resource_name = 'file'
authorization = Authorization()
Now you can upload the file to the `/API/V1/File/` path with the HTTP post request.
In this way, you can use Django-Tastypie to process files upload and download.You can use the above code and configuration for reference and adjustment to meet your project needs.