Login

All snippets

Snippet List

LoginAsForm - Login as any User without a password

Sometimes the only way to reproduce a bug on a production site is to login as the User who encountered it. This form allows you to login as any user on the site. **Usage** @staff_member_required def login_as(request, template="login_as.html"): data = request.POST or None form = LoginAsForm(data, request=request) if form.is_valid() form.save() return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL) ...

  • forms
  • login
  • auth
  • authenticate
  • form
  • auth.contrib
Read More

Custom nose runner

This method is replacement for django test runner. It uses nose test runner, which will discover all tests in the application (not even in tests module of applications). For installing put in settings.py: `TEST_RUNNER = 'nose_runner.run_tests'` where *nose_runner* is module containing the code

  • test
  • nose
  • testrunner
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

Captcha without Freetype or the Python Imaging Library (PIL)

If, like me, you've had trouble installing the [Python Imaging Library](http://www.pythonware.com/products/pil/) or [FreeType](http://freetype.sourceforge.org), you may have also had trouble getting a captcha to work. Here's my quick and dirty workaround — be warned, this is _very_ low level security, and shouldn't be used on high-profile sites. Originally published at <http://gregbrown.co.nz/code/django-captcha-without-freetype-or-pil/> Credit to the author of [django-simple-captcha](http://code.google.com/p/django-simple-captcha/), from whom I borrowed most of this code. ### Usage from django import forms from ABOVE-FILE import CaptchaField class CaptchaTestForm(forms.Form): myfield = forms.TextField() security_check = CaptchaField()

  • captcha
Read More

EditingMiddleware (quickly open views and templates in your text editor!)

Heavily based on [Snippet 1033](http://www.djangosnippets.org/snippets/1033/) and [Snippet 766](http://www.djangosnippets.org/snippets/766/). This snippet tracks what view and templates are used to render HTML responses and inserts a small dialog in the top right corner of the output with links to all the files. If your text editor support opening files from a browser protocol you can click the links to open the files right up! For example TextMate supports the `txmt://` protocol. Really saves some time if you find yourself editing a lot of templates. ![txt](http://img.skitch.com/20090602-11df2tfg273p99i95nfx6a74qe.png) **Usage** 1. Save this snippet in a file called `middleware.py` on your Python Path. 2. Add `middleware.EditingMiddleware` to your `MIDDLEWARE_CLASSES`. 3. Browse to any HTML page on your site!

  • middleware
  • editing
  • texteditor
  • editor
Read More

Sometimes Tag

Adds a templatetag that works like an if block, but . The one and only argument is a float that reflects the percentage chance. It defaults to .2, %20. {% sometimes %} <img src='spy_behind_sniper.jpg'/> {% else %} <img src='sniper.jpg'/> {% endsometimes %} -- or -- {% sometimes .001 %} You win! {% else %} Sorry, not a winner. Play again! {% endsometimes %} -- or -- {% sometimes .5 %} This shows up half the time. {% endsometimes %}

  • tag
  • random
Read More

partial tag

This is another partial tag, taken from a previous partial tag. The previous one assumed template locations by hardcoding in "partials/%s", etc. I took all that out, and made it work. And I took out the not needed third parameter for passing in your own data So now you call like this: {% partial "partials/mypartial.html" %} It passes the template context var into the partial, so anything you do in the main template, will work in the partial Just make sure you've got all the right imports.

  • tag
  • python
  • partial
Read More

split_contents2 for template tags

Is an updated way of splitting contents for a token, it does the split, but fixes the list.. EX: From a tag call like this: {% partial "partials/template.html" %} usually you get: ['partial','"partials/template.html"'] notice the " double quotes fixes it with: ['partial','partials/template.html'] takes out the " quotes

  • template
  • tag
  • python
Read More

Updated table tag

This table tag helps with render tables, which can be fairly complex. I updated the previous table tag (296). I added support for oddrow,evenrow,lastcellinrow,oddcol,evencol. And made a few minor adjustments to syntax formatting, and some non needed if conditionals These are all of the supported variables available in the context {{table.counter0}} {{table.counter}} {{table.rowcounter0}} {{table.rowcounter}} {{table.startrow}} {{table.endrow}} {{table.oddrow}} {{table.evenrow}} {{table.firstrow}} {{table.lastrow}} {{table.firstcell}} {{table.lastcell}} {{table.lastcellinrow}} {{table.evencol}} {{table.oddcol}} {{table.parenttable}}

  • template
  • tag
  • python
  • table
Read More

MySQL django password function

This functions encodes a password in the same format as django. You can set the auth_user.password column with the result of this function: update `auth_user`.`password` set `password` = django_password('secret') where id = 1234;

  • function
  • password
  • mysq
  • sha
Read More

Another Cookieless Session Middleware

the snippet improve juliocarlos's greate works(see [http://www.djangosnippets.org/snippets/1235/](http://www.djangosnippets.org/snippets/1235/) ) ,merge functtions to one middlewere class, fixed url regular expression and eliminate AJAX support etc... it's tested with django 1.0.2 and work fine on my wap site. * the middlewere must before SessionMiddlewar in MIDDLEWARE_CLASSES tuple eg: MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'middleware.cookieless_session.CookielessSessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', )

  • middleware
  • cookie
  • session
  • cookieless
Read More

Signal to notify new saved comments

Signal to notify new saved comments. **Example:** from django.contrib.comment import models, signals signals.comment_was_posted.connect(new_comment_notifier, sender=models.Comment)

  • django
  • python
  • comments
  • signals
Read More

Easy Conditional Template Tags

This is a conditional templatetag decorator that makes it *very* easy to write template tags that can be used as conditions. This can help avoid template boilerplate code (e.g. setting a variable in your template to be used in a condition). All you have to do is define a function with expected parameters that returns True or False. Examples are in the code.

  • template
  • tag
  • templatetag
  • if
  • conditional
  • condition
  • else
  • endif
Read More

3110 snippets posted so far.