Login

Most bookmarked snippets

Snippet List

Django Auth with JWT

This is an example of Django auth with JWT tokens, you can find how to add [jwt auth to Django Rest Framework in this tutorial](https://www.techiediaries.com/django-rest-framework-jwt-tutorial/)

  • authentication
  • auth
  • jwt
Read More

Dynamic Paginator Mixin

Dynamic Paginator Mixin for Django 1.8.* - 1.9.*, also work for CBV (Class Bassed View) but not for "django generic view".

  • django
  • pagination
  • paginator
Read More

Email authentication backend

Fixed minimal version, works with Django 1.7+, tested on Django 1.9. Add the following to your settings: AUTHENTICATION_BACKENDS = [ 'project.backends.UserModelEmailBackend', # Login w/ email 'django.contrib.auth.backends.ModelBackend', # Login w/ username ]

  • authentication
  • email
  • auth
  • username
  • backend
Read More

CBV: PreviewMixin

PreviewMixin adds a preview page for Django's CBV (FormView, UpdateView, CreateView). After a form has been submitted, it is returned again, optionally in a different template to confirm. If the form is submitted with the same data, the default "form_valid" function is executed. Features: 1. `process_preview` - function executed after submitting the form for the first time (default is to do nothing) 2. `done` - function for the action if the confirm page is sent (defaults to whatever form_valid of the django cbv does) 3. `preview_template_name` - variable with the name of the template for the confirm page (defaults to taking the same template as for the initial form) 4. new function `security_hash` to calculate a hash which is added to the confirmation form. Works kind of like django-formtools, just as a Mixin for the default Django cbv.

  • django
  • mixin
  • cbv
Read More

Multiple Model Forms feeding a single Form to use with a single FormView

This is a simple example of feeding multiple Forms into a single Form via its constructor method, to work with a single FormView and reap the benefits of Django's awesome Form validation system. It's a Form class that defines (or takes via an argument to its constructor) *parent* Forms (that can, for instance, be ModelForms, to take advantage of the automatic Field generation) and takes its fields from there. An advanced user won't be impressed by this, so excuse if this snippet is out of place, but a rather inexperienced user such as myself might find it interesting and make him willing to explore Django's internals a bit more.

Read More

One line SMTP sink server

Start simple SMTP server on localhost:25 and print to standard output all email headers and the email body. Useful for debugging outgoing mail without configuring SMTP daemon in development enviroment.

  • email
  • debug
  • smtp
  • mail
  • debugging
  • debuggingserver
Read More

ModelChoiceField with choice groups for recursive relationships

Note: must call within __init__() method, so you must do self.fields["field"] = ModelChoiseField(...). This is because I did not use a ModelChoiceIterator. A subclass of ModelChoiceField which represents the tree level of each node when generating option labels. It's limited to one level of nesting, if you need more, you should consider the django-mptt package. For example, where a form which used a ModelChoiceField: category = ModelChoiceField(queryset=Category.objects.all()) ...would result in a select with the following options: --------- Root 1 Root 2 Child 1.1 Child 1.2 Child 2.1 Using a NestedModelChoiceField instead: category = NestedModelChoiceField(queryset=Category.objects.all(), related_name='category_set', parent_field='parent_id', label_field='title') ...would result in a select with the following options: Root 1 --- Child 1.1 --- Child 1.2 Root 2 --- Child 2.1

  • ModelChoiceField
Read More

A basic view for working with jQuery dataTables plugin

jQuery dataTables is a fantastic and powerful client-side plugin, which has many capabilities. There are many libraries out there that easily integrate it with django with little to no effort. However, they provide everything out of the box, which isn't always the most flexible solution. I also think beginners have a lot to learn from not using already created tools. So, finding the general examples out there lacking, I though I'd hatch out a quick example of how to very basically integrate the two. I supply two lists at the top which define how to order the fields and which are searchable (quite similar to the one defined in the admin site) and later everything is figured from there. Of course, for a little more complex implementation (say using a method instead of a field) this will not work (since getattr doesn't automatically call a function if it is a function), but this snippet isn't supposed to provide everything but a very basic usage example, which can be very quickly expended in any way needed. Anyway, this snippet should work on all django versions and all dataTables versions without a hitch. last note- I use `cStringIO` with `json.dump` out of good personal experience with that settings, though it might not be the best way to serialize the data. Good luck, share use and enjoy, ~yuvi

  • django
  • dataTables
Read More

Lazy context processor.

Sometimes you have context variables that are needed on many pages in a site, but not all. You only want them to be evaluated when actually needed, especially if they are expensive calculations that do DB queries etc. The pattern to use is shown: put a callable into the context, not the final value, and also wrap the callable with memoize_nullary.

  • template
  • contextprocessor
  • lazy
  • context-processor
  • memoize
Read More

3110 snippets posted so far.