Login

3110 snippets

Snippet List

Easy file upload handler

This function emulates the file upload behaviour of django's admin, but can be used in any view. It takes a list of POST keys that represent uploaded files, and saves the files into a date-formatted directory in the same manner as a `FileField`'s `upload_to` argument.

  • image
  • forms
  • view
  • upload
  • imagefield
  • filefield
  • file
Read More

Bootstrap theme for django-endless-pagination?

An example for using bootstrap theme for django-endless-pagination or django-el-pagination as asked on [http://stackoverflow.com/questions/30149152/how-to-set-bootstrap-theme-for-django-endless-pagination](http://stackoverflow.com/questions/30149152/how-to-set-bootstrap-theme-for-django-endless-pagination) I used django-el-pagination to make it work on Django 1.9 and Bootstrap v4.0.0-alpha2, but same method should work on django-endless-pagination. For Bootstrap 3, you may consider removing extraneous classes in <li> and <a>.

  • bootstrap
  • django-endless-pagination
  • django-el-pagination
Read More

Bootstrap theme for django-endless-pagination?

An example for using bootstrap theme for django-el-pagination as asked on http://stackoverflow.com/questions/30149152/how-to-set-bootstrap-theme-for-django-endless-pagination I used django-el-pagination to make it work on Django 1.9, but same method should work on django-endless-pagination as well.

  • bootstrap
  • django-endless-pagination
  • django-el-pagination
Read More

Digg-like paginator, updated

This is an updated version of http://www.djangosnippets.org/snippets/628/ now working with Django's new Paginator class, instead of the deprecated ObjectPaginator. See: http://blog.elsdoerfer.name/2008/05/26/diggpaginator-update/

  • pagination
  • paginator
  • digg
Read More

Use DB Test Runner

**Use Case**: Specify the DB to run tests against. For example, use a legacy DB (un-managed), or a read-only DB where it is un-important to test creation, but important to test connection, trigger functions, and that models match schema. **Usage**: in DATABASES setting, add: 'TEST' :{ 'USEDB': 'your_test_DB_name_here', } and setting: `TEST_RUNNER = 'your_app.test_runner.UseDBTestRunner' ` Advantages over --keepdb: 1. DB specific setting for multi-DB setup (e.g., default can use normal test DB, while legacy can use a pre-existing table). 2. Can specify any DB table, including the one used by the app (for non-destructive tests, or dev DB) 3. Allows testing against DB where creation or copying is prohibitive.

  • testrunner
  • django-1.8
Read More

Django substitution user

django-substitution-user is a project that makes it possible to substitute user, if you logged in as superuser https://github.com/torchingloom/django-substitution-user

  • django
  • user
Read More
Author: TA
  • 0
  • 0

Readonly fields with link to change page in admin

Usage : class MyModelAdmin(ReadonlyLinksMixin, admin.ModelAdmin): readonly_fields_links = ('field1', 'field2') This adds a new ModelAdmin property (`readonly_fields_links`) that acts like the default `readonly_links` except that (if the field's type is a model that can be edited in the admin site) the value of the field has a link to the object. Same functionality as * [This snippet](https://www.djangosnippets.org/snippets/937/) * [and this one](https://www.djangosnippets.org/snippets/1008/) Except that it works without messing with the form that gets validated and saved, and thus without sometimes saving None values. It uses the documented property that `readonly_fields` can be callables ([Django doc](https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_display)) : the fields in `readonly_links_fields` are turned into callables that are appended to `readonly_links`. Each callable creates the linked value.

  • admin
Read More

UsernameField (for clean error messages)

This is a username field that matches (and slightly tightens) the constraints on usernames in Django's `User` model. Most people use RegexField, which is totally fine -- but it can't provide the fine-grained and user friendly messages that come from this field.

  • fields
  • forms
  • user
  • auth
  • form
  • field
  • username
  • users
  • authorization
Read More

ReadOnlyFieldsMixin to Form, ModelForm and Views (helper function)

### Usage: class Foo(models.Model): description = models.TextField() number = models.IntegerField() class FooOnlyDescriptionIsReadOnly(ReadOnlyFieldsMixin, forms.ModelForm): readonly_fields = ('description', ) class Meta: model = Foo fields = '__all__' class FooAllFieldsIsReadOnly(ReadOnlyFieldsMixin, forms.ModelForm): class Meta: model = Foo fields = '__all__' ### or use the function class FooForm(forms.ModelForm): class Meta: model = Foo fields = '__all__' ReadOnlyFooForm = new_readonly_form_class(FooForm, readonly_fields=('description', ))

  • readonly-form
  • ReadOnlyFieldsMixin
Read More

Django EncryptedField

Inspired by [Base64Field: base64 encoding field for storing binary data in Django TextFields](https://djangosnippets.org/snippets/1669/) but in a generic way. from django.db import models import base64 class Base64Encryptor(object): def encrypt(self, value): return base64.encodestring(value) def decrypt(self, msg): return base64.decodestring(msg) class MyModel(models.Model): ... b64_data = EncryptedField(encryptor=Base64Encryptor) ... # Usage my_obj = MyModel() my_obj.b64_data = "hello" print(my_obj.b64_data) # will output 'hello' print(my_obj.b64_data_enc) # will output 'aGVsbG8=\n'

  • django
  • fields
  • encryption
Read More