Login

Most bookmarked snippets

Snippet List

Use email addresses for user name for django 1.3

[Chris' code](http://djangosnippets.org/snippets/1845/) adapted to django 1.3. Basicly E-mail authorisation backend. Put it as one of your AUTHENTICATION_BACKENDS in settings.py: AUTHENTICATION_BACKENDS = ( 'community.auth.EmailBackend', )

  • email
  • auth
  • backend
  • authorisation
Read More

Multiple Select Widget with jQueryUI

This widget can be imported in your forms.py file and used like so: formfield = forms.ModelMultipleChoiceField(widget=MultiSelectWidget, queryset=Model.objects.all()) The javascript is provided by [Michael's Multiselect](http://github.com/michael/multiselect/tree/next). In addition to the javascript you will need [jQuery (the latest release is fine) and jQuery UI](http://jqueryui.com/). The convenient thing is that this widget will style to match whichever jQuery UI you've 'rolled' or selected, making it highly customizable! Hopefully this helps others who wanted a nice widget with jQuery and were sad to find there were no nice default options, enjoy! :)

  • jquery
  • widget
  • multiple select
  • multiselect
  • jqueryui
Read More

Drag and drop ordering of admin list elements using jQuery UI

Adds drag-and-drop ordering of rows in the admin list view. The only requirements is that the model has a field holding the position and that the field is made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'. The included javascript uses [jQuery UI's sortable](http://jqueryui.com/demos/sortable/) plugin Inspired by snippets [#1053](http://djangosnippets.org/snippets/1053) and [#998](http://djangosnippets.org/snippets/998/). Another similar snippet using AJAX is [#2047](http://djangosnippets.org/snippets/2047/).

  • admin
  • sort
  • jquery
  • order
  • sortable
Read More

Ajax ordering models on the change list page of the admin using drag and drop with jQuery UI

Makes models orderable on the change list page of the admin using drag and drop with jQuery UI (via sortable()). So you can order your objects in more easy way. Inspired by snippets [#1053](http://djangosnippets.org/snippets/1053/) and [#998](http://djangosnippets.org/snippets/998/) First, ordering field to your model (default called 'order). You can specify other name for this field, but you should add 'order_field' attr to model (i.e order_field = 'your_new_order_field_name') Also, snippet adds 'order_link' field to admin's changelist and hides it by javascript.

  • ajax
  • admin
  • jquery
  • ordering
Read More

login on activation with django-registration

Here's a signal handler to log a user in on registration activation. It took me an hour to figure out that I needed to put the user.backend in quotes and google wasn't being my friend. from [the django-registration documentation](http://docs.b-list.org/django-registration/0.8/faq.html): How do I log a user in immediately after registration or activation? You can most likely do this simply by writing a function which listens for the appropriate signal; your function should set the backend attribute of the user to the correct authentication backend, and then call django.contrib.auth.login() to log the user in.

  • login
  • auth
  • django-registration
Read More

Facebook shell

This adds an 'fbshell' management command which starts up a Python shell with an authenticated [pyfacebook](http://code.google.com/p/pyfacebook/) instance ready to make requests. This is very useful for testing out facebook requests or performing administration tasks without hooking a debugger into your application. This snippet should be saved to /yourproject/management/commands/fbshell.py See [custom management commands](http://docs.djangoproject.com/en/dev/howto/custom-management-commands/) for a description of how this works. If you are already using pyfacebook in your app then you'll already have the right settings, so just run : $ python manage.py fbshell A browser window will pop up, prompting you for authentication (unless you're already logged in to facebook). Press enter in the shell when you're finished this, and you'll be dropped into a shell with the session key, uuid, and name printed. Now you can use the facebook instance: >>> facebook.friends.get() >>> [...] If you haven't used pyfacebook in your app, you'll need at least the following settings in your settings.py FACEBOOK_API_KEY = 'your_api_key' FACEBOOK_SECRET_KEY = 'your_secret_key'

  • management
  • shell
  • facebook
  • command
Read More

Email or username authentication with masquerading

This backend will allow you to have users login using either their username or the email address as it is in the User model. In addition, it will allow anyone with the staff priveleges to login as another user. The method is to user the user you wish to masquerade as (either email/username) as the username and then a string of the format *username*/*password* as the password, where *username* is the username of the staff member, and *password* is their password.

  • authentication
  • email
  • login
  • auth
  • backend
Read More

Template context debugger with (I)Pdb

This allows you to set up a breakpoint anywhere in your template code, by simply writing {% pdb_debug %}. You can then access your context variables using context.get(..) at the pdb prompt. Optionally, install the ipdb package for colors, completion, and more (easy_install ipdb).

  • template
  • debug
  • context
  • pdb
  • ipdb
Read More

Multiple Choice model field

Usually you want to store multiple choices as a manytomany link to another table. Sometimes however it is useful to store them in the model itself. This field implements a model field and an accompanying formfield to store multiple choices as a comma-separated list of values, using the normal CHOICES attribute. You'll need to set maxlength long enough to cope with the maximum number of choices, plus a comma for each. The normal get_FOO_display() method returns a comma-delimited string of the expanded values of the selected choices. The formfield takes an optional max_choices parameter to validate a maximum number of choices.

  • multiple
  • model
  • form
Read More

iPhoneMiddleware

Adds an additional template dir to settings.TEMPLATE_DIRS if the request's HTTP_USER_AGENT string has the word iPhone in it. Allows you to easily create iPhone templates.

  • middleware
  • user-agent
  • iphone
Read More

Encryption Fields

This provides some basic cryptographic fields using pyCrypto. All encryption/decription is done transparently and defaults to use AES. Example usage: class DefferredJunk(models.Model): semi_secret = EncryptedCharField(max_length=255)

  • model
  • field
  • encryption
Read More

Timedelta Database Field

This subclass of django.models.Field stores a python datetime.timedelta object as an integer column in the database. It includes a TimedeltaFormField for editing it through djangos admin interface. Feedback welcome. 2011-04-20: TimedeltaField can now be (de)serialized. Tested with JSON. 2009-11-23: `_has_changed()` added. Before form.changed_data contained timedelta FormFields, even if nothing changed.

  • datetime
  • timedelta
  • dbfield
Read More

object-oriented generic views

Here's an example of writing generic views in an object-oriented style, which allows for very fine-grained customization via subclassing. The snippet includes generic create and update views which are backwards compatible with Django's versions. To use one of these generic views, it should be wrapped in a function that creates a new instance of the view object and calls it: def create_object(request, *args, **kwargs): return CreateObjectView()(request, *args, **kwargs) If an instance of one of these views is placed directly in the URLconf without such a wrapper, it will not be thread-safe.

  • views
  • generic
  • object
  • update
  • create
Read More

Login with email or username

A simple backend which allows you to login with either an email address or a username. It should be combined with another backend for checking permissions: AUTHENTICATION_BACKENDS = ( 'myproject.accounts.backends.EmailOrUsernameModelBackend', 'django.contrib.auth.backends.ModelBackend' )

  • email
  • login
  • auth
  • backend
Read More

3110 snippets posted so far.