Streaming large CSV files via admin actions
Based on https://djangosnippets.org/snippets/2020/ and https://stackoverflow.com/questions/5146539/streaming-a-csv-file-in-django Can be used on really large querysets.
- admin
- csv
- admin-actions
Based on https://djangosnippets.org/snippets/2020/ and https://stackoverflow.com/questions/5146539/streaming-a-csv-file-in-django Can be used on really large querysets.
Read the image url as base64 in django, this snippet usefull if you using the `django-easy-pdf` to solve this issue https://github.com/nigma/django-easy-pdf/issues/53 Usage example: ``` <img src="{{ profile.photo.url|read_image_as_base64 }}"> ```
Django zip longest templatetags to handle more than 3 arguments.
I separate this in two files, like this: export_excel.py and actions.py I tried to treat all possible forms of information that may appear in admin, such as properties, functions and normal fields, always getting the column name verbose_name or short_description depending on the case.
Templatetags function to convert the number into numberize method. It usefull to numberize the visitors, users, or other cases.
This snippet shows how to add a `url` field to your API objects, which will then show up as an object in your JSON output. As parameters, you can specify: - `urls`: A list of strings that exist on your URLMan class - `attribute`: The name of the URLMan class on your model, defaults to `"urls"` - `full`: If the full URLs including schema and hostname should be supplied, defaults to `True`
This middleware uses Django's Geoip support (https://docs.djangoproject.com/fr/2.2/ref/contrib/gis/geoip2/), as well as axes's package helper to retrieve IP address (since Django's REMOTE_ADDR might be wrong when behind a reverse proxy). Ensure your geolite DB files are up to date (eg. with https://djangosnippets.org/snippets/10674/). The checker is optional, but ensures that security is not broken due to a misspelled/missing GEOIP_COUNTRY_WHITELIST.
This management command updates country and city Geolite databases from Maxmind (binary databases, not CSV ones), for use with Django's builtin Geoip utilities.
Use this to display an object on a page AND be able to process a form on this page. This is just a copy of the [Django docs advice](https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/#using-formmixin-with-detailview), but put as a reusable, standalone View.
A form field for a Boolean that forces the user to make a choice from a list of choices. **Use Case** You have a Yes/No question the user must answer, but they may answer it yes or no. You don't want to supply a default because your need to force the user to actively select their answer. If they do not select an answer, the field should raise a validation error, like "This field is required". Normal BooleanField logic is based on a "checkbox", which, when "required" is required to be checked. This logic assumes that an empty value is the same as False -- in fact, there is no way for validators to distinguish between the empty value and False. Based on excellent suggestion from Peter DeGlopper: https://stackoverflow.com/a/56677670/1993525
Requires to install "basicauth" package, which does basic-auth header encoding/decoding cleanly according to RFCs. Could be improved to return a "realm" in case of http401, like in https://djangosnippets.org/snippets/1720/, although I'm not sure it's really useful in django usecases.
Django's CharField requires a max_length, and TextField displays a multi- line widget, but in Postgres there's no reason to add an arbitrary max thanks to the `varlena` storage format. So this is a TextField that displays as a single line instead of a multiline TextArea.
Use nginx sendfile (X-Accel-Redirect) function to serve files but pass traffic through django. Can be used to serve media files only to logged-in users.
### settings.py MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'utils.LoginRequiredMiddleware', ] LOGIN_REQUIRED_URLS = [ r'^panel/(.*)$' ] this will help any url under `panel/` require login.
I have a product table but don't have a brand table. brands in product.brand as denormalized and I want to choose brands from a select tag.