Login

Most bookmarked snippets

Snippet List

testshell

This commands runs a Python interactive interpreter with test database and data from the given fixture(s). It is usable if you want to play with test database. See also testserver docs

  • fixtures
  • shell
  • testshell
Read More

Language aware cache decorator

Caches a view based on the users language code, a cache_key and optional function arguments. The cache_key can be a string, a callable or None. If it's None, the the name of the decorated function is used. You can pass a tuple `func_args` of arguments. If passed, these arguments are part of the cache key. See examples for details.

  • cache
  • decorator
  • caching
Read More

Decorator to modify reverse() to render SSL urls

This snippet monkey-patches Django's reverse() method (use for generating URLs from vew functions and parameters) to allow certain areas of your site to automatically have URLs with the correct SSL domain in place. This saves you from having to use unnecessary redirects to guide users to an SSL-encrypted version of a page. This should still be used alongside a redirect-based method (such as [this snippet](http://www.djangosnippets.org/snippets/85/)) to ensure that the user can't access an unencrypted version of the page Simply add the code to the files mentioned in the code. This obviously won't work anywhere that doesn't use reverse(), the admin app seems to be an example of this.

  • url
  • ssl
  • reverse
  • permalink
Read More

MultipleEmailsField

This is a custom field for multiple emails separated by comma. Original code was replaced by code from Django documentation: http://docs.djangoproject.com/en/dev/ref/forms/validation/ (MultiEmailField) so i'm not the author of the code, but just put it here to replace an outdated solution. Uses code from mksoft comment http://www.djangosnippets.org/snippets/1093/

  • multiple
  • forms
  • email
  • field
Read More

Django Drag and Drop Using YUI

I was trying to implement the django inline drag & drop, I cudn't understand. Then I have try with YUI library Copy the above code in the respective .py & download the YUI library and add the JS in your media folder. This will do..., Try this our , it's easy to use..,

  • django
  • yui
  • drag
  • drop
  • and
Read More

Split a string to a list and add to select options

The template filter is use for split a string such as "foo|foobar|bar" to select option widget. You can define the splitter of the string by yourself. **Usage:** Add the code into templatetags folder of a installed app, then add below code into your template file. ` {% load split_as_option %} <select name="widget_name"> {{ QuerySet.values|split_as_option:"|" }} </select> `

  • template
  • filter
  • split
Read More

Time toggle on mouseover template filter

Shows the timesince to the user and then on mouseover it will display the exact time for allowing the user to see the exact time. Example: shows "about 1 day, 3 hours ago" and then changes on mouseover to read "at 3:05pm on Wednesday 22nd April 2009"

  • date
  • time
  • usability
  • time-toggle
Read More

Debug data for forms

I sometimes find that larger forms need data associated with them, but it's a bit of a pain to retype it each time when I'm debugging. The DebugForm base class lets you specify sets of testing data that will be inserted into your form if your project is in debug mode, randomly chosen from the DEBUG_DATA dict.

  • forms
  • debug
  • defaultdata
Read More

"Zoom in" on rendered HTML that the test client returns

If you have this as your base class for all unit tests you can do the following: class TestViews(BaseTestCase): def test_generated_stats(self): "test that certain stuff in the response" ...create some content for testing or use fixtures... response = self.client.get('/some/page/') # At this point response.content is a huge string filled with HTML tags and # "junk" that you don't need for testing the content thus making it difficult # to debug the generated HTML because it so huge. # So we can zoom in on the <div id="stats>...</div> node html = self._zoom_html(response.content, '#stats') # the variable 'html' will now be something like this: """ <div id="stats"> <p> <strong>2</strong> students<br/> <em>1</em> warning. </p> </div> """ # This makes it easier to debug the response and easier to test # against but the HTML might still be in the way so this would fail: self.assertTrue('2 students' in html) # will fail # To strip away all html use _strip_html() content = self._strip_html(html) # Now this will work self.assertTrue('2 students' in content) # will work It works for me and I find this very useful so I thought I'd share it.

  • css
  • test
  • client
  • lxml
  • lxml.html
Read More

Widget for DateTime values on Geraldo Reports

This is a widget for date/time fields on **Geraldo Reports**. When you use Geraldo to write reports, date/time fields must be formatted using **get_value** lambda attribute, because ObjectValue doesn't know what mask you want to use. With this widget, you just copy it into a common use Python file, import into your reports file and use it replacing ObjectValue on elements for fields you must be formatted as date/time format. **Example:** from geraldo import Report, ReportBand, ObjectValue, landscape from utils.reports import DateTimeObjectValue class ReportPhoneList(Report): title = u'Phone List' page_size = landscape(A4) class band_detail(ReportBand): height = 0.5*cm elements = [ ObjectValue(attribute_name='id', top=0.1*cm), DateTimeObjectValue(attribute_name='birth_date', left=26.2*cm, top=0.1*cm, format='%m/%d/%Y'), ]

  • geraldo
Read More

Soft hyphenation (&shy;) template filter using PyHyphen

This template filter is meant to insert soft hyphens ([&shy; entities](http://www.cs.tut.fi/~jkorpela/shy.html)) in text whever it can. For this is relies on a **recent** checkout of the [PyHyphen](http://code.google.com/p/pyhyphen/) interface to the hyphen-2.3 C library, which is also used by Mozilla and OpenOffice.org. It takes two optional parameters: the language to hyphenate in and the minimum word length to consider for hyphenation. If no language is given, the default language from the settings file is used. The second parameter defaults to 5 characters. Usage example: {% load hyphenation %} {{ object.text|hyphenate:"nl-nl,6" }}

  • template
  • filter
  • text
  • hyphenation
  • hyphen
  • soft
  • &shy;
  • pyhyphen
  • typografy
Read More

entity encoded email-address

entity encoded string for a somewhat safer email-address. this filter encodes strings to numeric entities, almost every standard-browsers decodes the entities and display them the right way. needless to say that bots are smart, so this is not a 100% guaranteed spam prevention.

  • email
  • spam
  • prevention
Read More

nofollow filter for external links

Based on [svetlyak](http://www.djangosnippets.org/users/svetlyak/)'s [nofollow filter](http://www.djangosnippets.org/snippets/312/). This one processes only external URL's. Links with internal URL's will be returned unmodified. There's one gotcha; I preferred to have false positives instead of false negatives. So, it will nofollow `href="some/relative/path/"` for example. If you must do relative paths do it like this: <a href="./some/relative/path">link text</a>

  • filter
  • nofollow
Read More

3110 snippets posted so far.