Skip to content

Commit 39e2306

Browse files
julzhkcodingjoe
authored andcommitted
Update Readme.md
More extensive edit for clarity and readability
1 parent c2aa355 commit 39e2306

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

README.md

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@
44

55
# Django Standardized Image Field
66

7-
Django Field that implement the following features:
7+
## Why would I want this?
88

9-
* Django-Storages compatible (S3)
10-
* Resize images to different sizes
9+
This is a drop-in replacement for the [Django ImageField](https://docs.djangoproject.com/en/1.8/ref/models/fields/#django.db.models.ImageField) that provides a standardized way to handle image uploads.
10+
It is designed to be as easy to use as possible, and to provide a consistent interface for all image fields.
11+
It allows images to be presented in various size variants (eg:thumbnails, mid, and hi-res versions)
12+
and it provides a way to handle images that are too large with validators.
13+
14+
15+
## Features
16+
17+
Django Standardized Image Field implements the following features:
18+
19+
* [Django-Storages](https://django-storages.readthedocs.io/en/latest/) compatible (eg: S3, Azure, Google Cloud Storage, etc)
20+
* Resizes images to different sizes
1121
* Access thumbnails on model level, no template tags required
12-
* Preserves original image
13-
* Asynchronous rendering (Celery & Co)
14-
* Restrict accepted image dimensions
15-
* Rename files to a standardized name (using a callable upload_to)
22+
* Preserves original images
23+
* Can be rendered asynchronously (ie as a [Celery job](https://realpython.com/asynchronous-tasks-with-django-and-celery/))
24+
* Restricts acceptable image dimensions
25+
* Renames file to a standardized name format (using a callable `upload_to` function, see below)
1626

1727
## Installation
1828

19-
Simply install the latest stable package using the command
29+
Simply install the latest stable package using the following command
2030

2131
```bash
2232
pip install django-stdimage
@@ -28,11 +38,13 @@ and add `'stdimage'` to `INSTALLED_APP`s in your settings.py, that's it!
2838

2939
## Usage
3040

41+
Now it's instally you can use either: `StdImageField` or `JPEGField`.
42+
3143
`StdImageField` works just like Django's own
3244
[ImageField](https://docs.djangoproject.com/en/dev/ref/models/fields/#imagefield)
33-
except that you can specify different sized variations.
45+
except that you can specify different size variations.
3446

35-
The `JPEGField` works similar to the `StdImageField` but all size variations are
47+
The `JPEGField` is the same as the `StdImageField` but all images are
3648
converted to JPEGs, no matter what type the original file is.
3749

3850
### Variations
@@ -58,7 +70,7 @@ class MyModel(models.Model):
5870
# is the same as dictionary-style call
5971
image = StdImageField(upload_to='path/to/img', variations={'thumbnail': (100, 75)})
6072

61-
# variations are converted to JPEGs
73+
# JPEGField variations are converted to JPEGs.
6274
jpeg = JPEGField(
6375
upload_to='path/to/img',
6476
variations={'full': (None, None), 'thumbnail': (100, 75)},
@@ -77,23 +89,40 @@ class MyModel(models.Model):
7789
}, delete_orphans=True)
7890
```
7991

80-
For using generated variations in templates use `myimagefield.variation_name`.
92+
To use these variations in templates use `myimagefield.variation_name`.
8193

8294
Example:
8395

8496
```html
8597
<a href="{{ object.myimage.url }}"><img alt="" src="{{ object.myimage.thumbnail.url }}"/></a>
8698
```
8799

88-
### Utils
100+
### Upload to function
101+
102+
You can use a function for the `upload_to` argument. Using [Django Dynamic Filenames][dynamic_filenames].[dynamic_filenames]: https://github.com/codingjoe/django-dynamic-filenames
89103

90-
Since version 4 the custom `upload_to` utils have been dropped in favor of
91-
[Django Dynamic Filenames][dynamic_filenames].
104+
This allows images to be given unique paths and filenames based on the model instance.
92105

93-
[dynamic_filenames]: https://github.com/codingjoe/django-dynamic-filenames
106+
Example
107+
108+
```python
109+
from django.db import models
110+
from stdimage import StdImageField
111+
from dynamic_filenames import FilePattern
112+
113+
upload_to_pattern = FilePattern(
114+
filename_pattern='my_model/{app_label:.25}/{model_name:.30}/{uuid:base32}{ext}',
115+
)
116+
117+
118+
class MyModel(models.Model):
119+
# works just like django's ImageField
120+
image = StdImageField(upload_to=upload_to_pattern)
121+
```
94122

95123
### Validators
96-
The `StdImageField` doesn't implement any size validation. Validation can be specified using the validator attribute
124+
The `StdImageField` doesn't implement any size validation out-of-the-box.
125+
However, Validation can be specified using the validator attribute
97126
and using a set of validators shipped with this package.
98127
Validators can be used for both Forms and Models.
99128

@@ -120,9 +149,9 @@ Django [dropped support](https://docs.djangoproject.com/en/dev/releases/1.3/#del
120149
for automated deletions in version 1.3.
121150

122151
Since version 5, this package supports a `delete_orphans` argument. It will delete
123-
orphaned files, should a file be delete or replaced via Django form or and object with
124-
a `StdImageField` be deleted. It will not be deleted if the field value is changed or
125-
reassigned programatically. In those rare cases, you will need to handle proper deletion
152+
orphaned files, should a file be deleted or replaced via a Django form and the object with
153+
the `StdImageField` be deleted. It will not delete files if the field value is changed or
154+
reassigned programatically. In these rare cases, you will need to handle proper deletion
126155
yourself.
127156

128157
```python
@@ -141,10 +170,10 @@ class MyModel(models.Model):
141170

142171
### Async image processing
143172
Tools like celery allow to execute time-consuming tasks outside of the request. If you don't want
144-
to wait for your variations to be rendered in request, StdImage provides your the option to pass a
145-
async keyword and a util.
146-
Note that the callback is not transaction save, but the file will be there.
147-
This example is based on celery.
173+
to wait for your variations to be rendered in request, StdImage provides you the option to pass an
174+
async keyword and a 'render_variations' function that triggers the async task.
175+
Note that the callback is not transaction save, but the file variations will be present.
176+
The example below is based on celery.
148177

149178
`tasks.py`:
150179
```python
@@ -177,19 +206,18 @@ def image_processor(file_name, variations, storage):
177206
class AsyncImageModel(models.Model):
178207
image = StdImageField(
179208
# above task definition can only handle one model object per image filename
180-
upload_to='path/to/file/',
209+
upload_to='path/to/file/', # or use a function
181210
render_variations=image_processor # pass boolean or callable
182211
)
183212
processed = models.BooleanField(default=False) # flag that could be used for view querysets
184213
```
185214

186215
### Re-rendering variations
187-
You might want to add new variations to a field. That means you need to render new variations for missing fields.
216+
You might have added or changed variations to an existing field. That means you will need to render new variations.
188217
This can be accomplished using a management command.
189218
```bash
190219
python manage.py rendervariations 'app_name.model_name.field_name' [--replace] [-i/--ignore-missing]
191220
```
192221
The `replace` option will replace all existing files.
193-
The `ignore-missing` option will suspend missing source file errors and keep
194-
rendering variations for other files. Otherwise command will stop on first
195-
missing file.
222+
The `ignore-missing` option will suspend 'missing source file' errors and keep
223+
rendering variations for other files. Otherwise, the command will stop on first missing file.

0 commit comments

Comments
 (0)