Django Widget¶
Settings¶
Besides required pub_key
, secret
settings there are optional settings,
for example, widget_version
or widget_variant
:
UPLOADCARE = {
'pub_key': 'demopublickey',
'secret': 'demoprivatekey',
'widget_version': '2.5.0',
'widget_variant': 'min', // without jQuery
'cdn_base': 'https://cdn.mycompany.com',
}
PyUploadcare takes assets from Uploadcare CDN by default, e.g.:
<script src="https://ucarecdn.com/widget/x.y.z/uploadcare/uploadcare.full.min.js"></script>
If you don’t want to use hosted assets you have to turn off this feature:
UPLOADCARE = {
# ...
'use_hosted_assets': False,
}
In this case local assets will be used.
If you want to provide custom url for assets then you have to specify widget url:
UPLOADCARE = {
# ...
'use_hosted_assets': False,
'widget_url': 'http://path.to/your/widget.js',
}
Uploadcare widget will use default upload handler url, unless you specify:
UPLOADCARE = {
# ...
'upload_base_url' = 'http://path.to/your/upload/handler',
}
Model Fields¶
As you will see, with Uploadcare, adding and working with a file field is just as simple as with a TextField. To attach Uploadcare files to a model, you can use a FileField or ImageField. These fields play by common Django rules. South migrations are supported.
Note
When you call your_model_form.is_valid()
or call photo.full_clean()
directly it invokes File.store()
method automatically. In other cases
you should store objects manually, e.g:
photo.photo_2x3 = File('a771f854-c2cb-408a-8c36-71af77811f3b')
photo.save()
photo.photo_2x3.store()
FileField¶
FileField
does not require an uploaded file to be any certain format.
from django.db import models
from pyuploadcare.dj import FileField
class Candidate(models.Model):
resume = FileField()
ImageField¶
ImageField
requires an uploaded file to be an image. An optional parameter
manual_crop
enables, if specified, a manual cropping tool: your user can
select a part of an image she wants to use. If its value is an empty string,
the user can select any part of an image; you can also use values like
"3:4"
or "200x300"
to get exact proportions or dimensions of resulting
image. Consult widget documentation regarding setting up the manual crop:
from django.db import models
from pyuploadcare.dj import ImageField
class Candidate(models.Model):
photo = ImageField(blank=True, manual_crop="")
Advanced widget options¶
You can pass any widget options via FileWidget
‘s attrs argument:
from django import forms
from pyuploadcare.dj.forms import FileWidget, ImageField
# optional. provide advanced widget options: https://uploadcare.com/documentation/widget/#configuration
class CandidateForm(forms.Form):
photo = ImageField(widget=FileWidget(attrs={
'data-cdn-base': 'https://cdn.super-candidates.com',
'data-image-shrink': '1024x1024',
}))
FileGroupField¶
FileGroupField
allows you to upload more than one file at a time. It stores
uploaded files as a group:
from django.db import models
from pyuploadcare.dj import FileGroupField
class Book(models.Model):
pages = FileGroupField()
ImageGroupField¶
ImageGroupField
allows you to upload more than one image at a time.
It stores uploaded images as a group:
from django.db import models
from pyuploadcare.dj import ImageGroupField
class Gallery(models.Model):
photos = ImageGroupField()