Login

Most bookmarked snippets

Snippet List

Dynamic DEBUG setting for PyCharm

1. Next to the Play button you can Edit Configuration 2. Click the green + on the left, add 2 "Django Server". Call one DEBUG, call the other RUN. 3. Add "DEBUG False" to RUN in its "Environment variables" 4. Add "DEBUG True" to DEBUG in its "Environment variables" (make sure the port # is different from RUN!) Change `DEBUG = True` in `settings.py` to the below code Go back to the main IDE window and either select RUN or DEBUG and go to localhost/404 to either see the dev debug 404 or your custom `404.html` (that you obviously previously made in the `/templates/` folder ;)

  • PyCharm
  • DEBUG
  • IDE
  • Dynamic
Read More

django smartround filter

rounds floats in more human readable format e.g. 341.123434 mUSD -> 341mUSD 0.45345345 mUSD -> 0.5 mUSD 0.034545 mUSD -> 0.03 mUSD 0.0014545 mUSD -> 0.001 mUSD etc.

  • smart round
  • smartfloatformat
Read More

Custom DRF browsable API interface for InBBoxFilter of django-rest-framework-gis

[DRF browsable API interface](http://www.django-rest-framework.org/api-guide/filtering/#customizing-the-interface) for django-rest-framework-gis [InBBoxFilter](https://github.com/djangonauts/django-rest-framework-gis#inbboxfilter) Tested with Django==1.10.5 django-filter==1.0.1 djangorestframework==3.5.4 djangorestframework-gis==0.11 ![Screenshot](http://joxi.ru/8AnzxLDTOEgeAO.png)

  • django
  • django-rest-framework
  • django-rest-framework-gis
Read More

replace

Small example of how to write your own function. This is not available in Django. The function just replaces static text strings, regular expressions are not supported. The syntax is the same in SQLite, PostgreSQL, MySQL and Oracle.

  • text
  • replace
Read More

CNPJ and CPF Validation for Models

The code was placed inside a helper file without using a class. The Django validator was not designed to work with validator classes, it would appear, so retrieving the value from the field proved to be a hassle. Just create a helper file, import it on your model, and use the validator in the standard way, as such: cnpj = models.CharField(unique=True, max_length=14, validators=[validate_CNPJ]) cpf = models.CharField(unique=True, max_length=14, validators=[validate_CPF])

  • model
  • validation
  • cnpj
  • cpf
Read More

Simply Gravatar Templatetags

Simply Gravatar Templatetags, for example the name of this templatetag is: `templatetags/gravatar_tags.py`, this supported for Python2 or Python3.

  • django
  • image
  • templatetag
  • gravatar
Read More

CSVField for forms

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'])

  • forms
  • csv
  • field
Read More

A streamable "export to CSV" admin action

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

  • admin
  • export
  • csv
  • action
  • stream
  • export-csv
Read More

Mixin to enable admin permissions without using PermissionsMixin and models

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.

  • admin
  • mixin
  • permissions
Read More

3110 snippets posted so far.