Login

Snippets by fahhem

Snippet List

Class-Based AJAX fallback view

If you're used to auto_render like I am (and think it's an awesome way to DRY code) and you want to support users with and without javascript enabled, this class is perfect. You will need to provide the format requested, either through adding "(\.format)?" at the end of the url or through some middleware, but once you've done so this class will take care of rendering through the given template for normal users, JSON or JSONP for AJAX users. From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)

  • ajax
  • view
  • json
  • class
  • fallback
  • class-based
Read More

Resolve URLs to view name and args/kwargs

This is an expanded version of ["Resolve URLs to view name"](http://djangosnippets.org/snippets/1378/) without the monkey-patching. Simply pass in a URL such as '/events/rsvp/some_conference/' and you'll get back the view name or function (if name isn't available) and the arguments to it, eg 'events_rsvp', [], {'event_slug':'some_conference'}. Example (blatantly copied from previous snippet): === urlconf ==== urlpatterns = patterns('' (r'/some/url', 'app.views.view'), url(r'/some/other/(?P<url>\w+)', 'app.views.other.view', name='this_is_a_named_view'), url(r'/yet/another/(?P<url>\w+)/(\d+)', 'app.views.yetanother.view', name='one_with_args'), ) === example usage in interpreter === >>> from some.where import resolve_to_name >>> print resolve_to_name('/some/url') ('app.views.view',[],{}) >>> print resolve_to_name('/some/other/url') ('this_is_a_named_view',[],{'url':'url'}) >>> print resolve_to_name('/yet/another/url/5') ('one_with_args',[5],{'url':'url'}) From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)

  • url
  • resolve
  • name
  • args
  • kwargs
Read More

Encode emails as URIs

Put this snippet in a file in a templatetags/ directory and load it in your templates. Then use it to encode your emails: `{{"[email protected]"|html_encode_email}}` Or if you want some control over the anchor tag: `<a href="mailto:{{"[email protected]"|html_encode}}&subject=Feedback">Send Feedback</a>` From [fahhem.com](http://fahhem.com/) and [Recrec Labs](http://recreclabs.com/)

  • templatetag
  • email
  • spam
  • encode
Read More

fahhem has posted 3 snippets.