Login

3110 snippets

Snippet List

401 HttpResponse

This is a HTTP response is use in my application when I want to return a 401. It's pretty simple, but effective for my needs.

  • 401
  • httpresponse
Read More

Model manager with row caching

RowCacheManager is a model manager that will try to fetch any 'get' (i.e., single-row) requests from the cache. ModelWithCaching is an abstract base model that does some extra work that you'll probably want if you're using the RowCacheManager. So to use this code, you just need to do two things: First, set objects=RowCacheManager() in your model definition, then inherit from ModelWithCaching if you want the invalidation for free. If you are unusually brave, use the metaclass for ModelWithCaching and you won't even need the "objects=RowCacheManager()" line.

  • get
  • model
  • manager
  • model-inheritance
  • caching
Read More

Delicious Tag

Just add it in templatetags/delicious.py In your template: <h3>Del.icio.us</h3> <ul class="list"> {% load delicious %} {% load_delicious_links %} {% for link in delicious_links %} <li><a href="{{link.link}}">{{link.title|safe}}</a></li> {% endfor %} </ul>

  • tag
  • delicious
Read More

Pygmentation

Usage: (if you save it as pigmentation.py as I did) {% load pigmentation %} {% autoescape off %} {{ somevariable|pygmentize }} {% endautoescape %} There already a few of this code around, but this one is pretty clean, and includes css. It also works in both the development server and Dreamhost (python2.4 in my django config) without any unicode problems.

  • filter
  • tag
  • templatetag
  • pygments
  • code
  • colorize
  • color
Read More

Export Models

Warning: This python script is designed for Django 0.96. It exports data from models quite like the `dumpdata` command, and throws the data to the standard output. It fixes glitches with unicode/ascii characters. It looked like the 0.96 handles very badly unicode characters, unless you specify an argument that is not available via the command line. The simple usage is: $ python export_models.py -a <application1> [application2, application3...] As a plus, it allows you to export only one or several models inside your application, and not all of them: $ python export_models.py application1.MyModelStuff application1.MyOtherModel Of course, you can specify the output format (serializer) with the -f (--format) option. $ python export_models.py --format=xml application1.MyModel

  • tool
  • dump
  • serialization
  • export
  • db
  • database
Read More

activation_required

Simple decorator that checks for authentication and activation of account and redirect to login or activation page if needed Your ulrsconf file must have named urls with parameters that you call that decorator Dont forget to import reverse function from django.core.urlresolvers import reverse

  • decorator
  • auth
Read More

Custom DateField To Handle Credit Card Exp Date. Format: MM/YY

As users would login to their accounts to update their CC info, the expiration date always threw them off. The default format for displaying a datetime.date object is >YYYY-MM-DD Obviously the expiration date on your credit card uses the MM/YY format. I finally got around to creating a custom field/widget to handle this particular piece of data. Use like so... class CustomerForm(forms.ModelForm): cc_exp = DateFieldCCEXP() class Meta: model = Customer

  • datefield
  • credit-card
  • expiration-date
Read More
Author: pjs
  • 1
  • 3

easy absolute path for settings.py

when you deploy djangos apps, some servers have problems resolving the absolute path of some files (e.g: sqlite3 + lighttpd + apache), using the snippet above solves this issue :)

  • settings
Read More

EditInline for GenericForiegnKey II

This is an update to [snippet 765](http://www.djangosnippets.org/snippets/765/) as I was having trouble getting it to work on branches/newforms-admin @ r7771. There are just a few minor changes to the previous snippet, all simple stuff. I went ahead an added can_delete and can_order options that the previous snippet didn't include. [More details in blog post](http://paltman.com/2008/06/29/edit-inline-support-for-generic-relations/).

  • admin
  • foreignkey
  • generic
  • edit-inline
Read More

WithTag Tag

Set a context variable with the returned value by any templatetag. Useful for example in order to use url templatetag inside blocktrans: ` {% withtag url my_app.views.my_view as my_view_url %}` ` {% blocktrans %}` ` Click <a href="{{ my_view_url }}">here</a>` ` {% endblocktrans %}` ` {% endwithtab %}` Or with include templatetag: ` {% withtag include "js_template.js" as js_template %}` ` {{ js_template }}` ` {% endwithtab %}` It works properly with your own custom templatetags.

  • templatetag
  • with
  • withtag
  • with_tag
Read More

Template tag for compressed CSS & JS (GAE version)

Extension of the idea from [WuzHere example from Google IO](http://code.google.com/p/wuzhere/) about creating one compressed js or css file. Original code used not very elegant if statements. I've changed it to template tags. On production server it will also use current version id. Insert code in *cssjs.py* file in *templatetags* dir in your application and use as below (more details in docs): `<script type="text/javascript" src="/media/jsmergefile.js"></script>` **This code does not compress CSS and JS on the fly, because GAE is read-only and using Datastore is too heavy.**

  • templatetag
  • google-app-engine
Read More

ActiveManager: filter objects depending on publication and/or expiration dates

This manager is intended for use with models with publication and/or expiration dates. Objects will be retrieved only if their publication and/or expiration dates are within the current date. Use is very simple: class ExampleModel(models.Model): publish_date = models.DateTimeField() expire_date = models.DateTimeField(blank=True, null=True) objects = models.Manager() actives = ActiveManager(from_date='publish_date', to_date='expire_date') ExampleModel.actives.all() # retrieve active objects according to the current date The manager works correctly with nullable date fields. A null publication date means "*always published (until expiration date)*" and a null expiration date means "*never expires*". Most models should define the `objects` manager as the default manager, because otherwise out of date objects won't appear in the admin app.

  • model
  • manager
  • active
  • publication
  • expiration
  • date-filter
Read More