Resize or Modify an image before saving
Small snippet that will resize all images before they uploaded to the server.
- image
- pil
- resize
- save
- imagefield
- modified
- pre_save
- override
Small snippet that will resize all images before they uploaded to the server.
FileField that checks that the file is a valid CSV and if specified in `expected_fieldnames` checks that the fields match exactly. The widget's `accept` parameter is set to accept csv, text and excel files. **TODO**: Validate the entirety of the CSV file, not just the headers. But this should be enough for most use cases, as checking the whole file could be computationally expensive for huge files. Example usage: people = CSVField(expected_fieldnames=['First Name', 'Last Name'])
This snippets is inspired from [#2995](https://djangosnippets.org/snippets/2995/) but add the following: * get rid of `singledispatch` so we can use it with python2 without pip installing anything * streaming capabilities * the configuration params (header, fields, exclude...) are passed to the action function so we don't pollute the ModelAdmin class * fix utf8 issue in header
Django 1.10 Internationalization Middleware, more complete tutorial: https://python.web.id/blog/how-to-implement-django-internationalization/
Implements necessary permission checks on a user model to be compatible with django admin, but just return true on all permissions without actually checking it against anything. Useful when you have a user model that should always be allowed to use django admin, and you don't care about using django's own PermissionsMixin and don't want to have those models added to your database.
Say you want to keep your API secure and thus it has authentication, but there's this one View action in a ViewSet which unlike the rest of the ViewSet's actions needs to allow free access without authentication. This solution applies the good old `IsAuthenticated` permission to all ViewSet actions except those defined in a `login_exempt_actions` list. That's a list of the ViewSet action's names. This is a simple solution for this particular problem, which I imagine could be quite common. Any case where the requirements are more complex should implement one of the DRF permissions extensions which allow for the use of logical operators. **NOTE**: Remember that `request.user` will be an `AnonymousUser` instance, so be careful with any code which assumes it'll be a `User` instance. This could be the case with, say, a custom `get_queryset` implementation.
This is a modificated version of `CachedPaginator` by **daniellindsley** [https://djangosnippets.org/snippets/1173/](https://djangosnippets.org/snippets/1173/) ([web-arhive-link](https://web.archive.org/web/20150927100427/https://djangosnippets.org/snippets/1173/)). Which not only cache `result_objects`, but the `total_count` of the `queryset` too (usefull if computating the count is an expensive operation too).
Rough check for unused methods in our apps. Scans models, views, utils, api, forms and signals files for what look like methods calls, then looks at all of our classes' methods to ensure each is called. Do not trust this blindly but it's a good way to get leads on what may be dead code. Assumes a setting called `LOCAL_APPS` so it only bothers to look at the code you've written rather than everything in `INSTALLED_APPS`.
The version of snippet that works with Django 1.9
This is a revised version of https://djangosnippets.org/snippets/2921/
Filter to remove words at the end of a string Example: Myvar: "My name is Arthur and django si awesome" {{myvar|wordend:4}} Output: "My name is Arthur"
Filter to remove words at the beginning of a string Example: Myvar: "My name is Arthur and django si awesome" {{myvar|wordremoveb:5}} Output: "django is awesome"
If your model have two dates, start and end of something, you may want to have filter which allow you to show objects which last on specific day.
# Django AJAX Form View ### A simple example for an AJAX-powered view
Dynamic Paginator Mixin for Django 1.8.* - 1.9.*, also work for CBV (Class Bassed View) but not for "django generic view".
3110 snippets posted so far.