Login

All snippets written in Python

Snippet List

Get sorted items for any model in any order

This get_sorted_items tag takes app.model, a number n, a a field name to sort ,and a variable-name as arguments and will deliver the n first objects of the model. it checks if a Manager *publicmgr* exists and calls this if the user isn't authenticated. Additionally it write the count of the model to the context

  • template
  • tag
  • dry
Read More

Wrappable text

Sometimes you have an uncontrolled amount of text in a horizontally constrained space. The wrappable filter places zero-width breaking spaces into the given text so that it can wrap at any point, as necessary for the containing width. Sometimes better than eliding (chopping long text...) or cropping/scrolling overflow.

  • template
  • filter
  • typography
Read More

django on tornado

there have been many posts on running django on tornado with static media served by nginx. But for dumb people like me, the whole thing needs to be spelt out. So here is how I succeeded in serving django from a virtual host using nginx and tornado. The key thing to note is that 'root' refers to the **parent** directory of the root and not the full path. Also remember to put in ':' as a line end. Procedure - start the tornado server with the python script on localhost:8888, start nginx. Relax and enjoy your django at the speed of light. Nginx can be got by apt-get or yum, but you need the latest git clone of Tornado - the default tarball does not support django. btw, this install is for FC11 on my laptop - I have done it in production on lenny.

  • nginx
  • tornado
Read More

GzipFileSystemStorage

`GzipFileSystemStorage` is a `FileSystemStorage` subclass that transparently compresses files. [More Info](http://theidioteque.net/blog/2009/9/29/gzipfilesystemstorage/)

  • compression
  • file
  • gzip
  • storage
  • compress
Read More

Field value as plain text which can't be edited by user

Shows field value as plain text which can't be edited by user. Field value (or key value for foreign keys) is stored in hiddden input. Value of field is stored in hidden input and current value is placed as plain text. User can't change it's value. If field is foreign key then additional attribute 'key' should be set to True (key is stored in hidden field and unicode value is visible): self.fields['my_field'].widget = HiddenInputWithText(attrs={ 'key' : True }) There is one condition: for foreign key field its name have to be same as lowercased model name. Default 'key' value - False is correct for non-foreign key fields: self.fields['my_field'].widget = HiddenInputWithText()

  • forms
  • hidden
  • widget
  • input
Read More

Render arbitrary models - template tag

This template tag provides an easy way to render objects in your template, even if you don't know ahead of time what type they are. For example, if you've got a search page with a result list comprised of objects from various models, you can simply loop through them and render them using the tag. The tag will choose the best template and render the object for you. The tag's docstring has all the details. I hope you find this as useful as I have. Questions, comments, complaints welcome.

  • template
  • tag
  • model
  • render
  • display
Read More

View decorator to convert DoesNotExist (ObjectDoesNotExist) exceptions into Http404 exceptions

A decorator that can be applied to your views to turn ObjectDoesNotExist exceptions into Http404 exceptions. This means people will see a "Page not found" error rather than an "Internal Server Error" when they are request something that does not exist in the database. Example: @raise_404 def show_event(request, id): event = Event.objects.get(id) return render_to_response('events/show_event.html', { 'event' : event })

  • error
  • exception
  • 404
  • errors
  • exceptions
  • objectdoesnotexist
  • doesnotexist
Read More

Integer based MoneyField

It is supposed the aggregation on integer is fast than numeric type in database duo to how they are stored as numeric is represented as string. As money only have 2 decimal place, it can be converted to an Integer despite of its decimal point. The python class decimal.Decimal also has a shortcoming that it is not json serializable(neither cjson nor simplejson). This money field appears as a float number to the front end, so it does not meet the headache as DecimalField. The usage is very simple. In Model: class SomeModel(models.Model): ... income = MoneyField('income') ... Then treat the attribute of the model instance as a normal float type. **Notes** If you try to use aggregation on this field, you have to convert it to float by yourself. Currently I could not find any method to fix this.

  • money
  • db
  • field
Read More
Author: Jay
  • 0
  • 1

Simple REST APIs

This is how I tend to roll simple REST APIs. It's by no means as full-featured as something like Piston, but it's simple, clean, and it works.

  • rest
Read More

simple tag-cloud Template Tag

This Template Tag computes the font-size of two given arguments and returns a CSS-encoded string like "font-size: XXpx;", which can be used to format the font-size of link. (The minium font-size must be set with CSS.) Requires two arguments: 1. occurrence of the current tag 2. sum of all occurrences It works great for my tag-cloud. [Source of the logarithmic formula](http://www.php.de/php-fortgeschrittene/44928-tag-cloud-algorithmus-fuer-schriftgroessye.html) **Usage** `<a href="http://www.anything.com" {% cloudify variable.occurrence overall. occurrence_sum %} title="anything">any tag</a>`

  • tag
  • cloud
  • logarithmic
Read More

Twitterfy

Improved version of my snippet #1346. Now works correctly with multiple usernames and hash tags. Both twitter usernames and hashtags are converted into links to twitter profiles and twitter search. Updated, forgot about underscores in usernames.

  • template-filter
  • twitter
  • hash-tag
  • at-reply
Read More

ContentType Form

When working with the ContentType model there are generally two issues. 1. Models are listed in the table but not imported. 2. Unicode only returns model not application so being able to select a a app/model is sometimes difficult when to applications have a model with the same name. This snippet gets a listing of imported models and creates a drop down for selection. I also included a function that uses the returned from to get and save the correct ContentType within the primary model.

  • form
  • contenttype
Read More

2956 snippets posted so far.