Hi, I'm Daniel Greenfeld, and welcome to my blog. I write about Python, Django, and much more.

Exceptions as Decorator Arguments

Tuesday, December 24, 2013 (permalink)

I wanted to see if I could have an exception as a decorator argument. Here is what I came up with:

import functools

class MyGoof(Exception):
    pass

def pass_goof(exception):
    def decorator(test_func):
        @functools.wraps(test_func)
        def wrapper(*args, **kwargs):
            try:
                return test_func(*args, **kwargs)
            except exception as e:
                return ...
Read more ...

The Easy Form Views Pattern Controversy

Friday, May 10, 2013 (permalink)

In the summer of 2010 Frank Wiles of Revsys exposed me to what I later called the "Easy Form Views" pattern when creating Django form function views. I used this technique in a variety of places, including Django Packages and the documentation for django-uni-form (which is rebooted as django-crispy-forms). At ...

Read more ...

Filepicker.io and South

Tuesday, April 23, 2013 (permalink)

I've heard good things about filepicker.io, which is a service that makes file uploading a much better experience. Unfortunately, the Django package for filepicker.io doesn't work with South. When I try to create a migration using the filepicker.io field using code like the following...

# products ...
Read more ...

Generating NCX files with Python

Monday, April 15, 2013 (permalink)

With the help of fellow Python developer Matt Harrison's excellent Ebook Formatting: KF8, Mobi & EPUB, we managed to create pretty decent looking Kindle and ePub versions of Two Scoops of Django.

One of many things we did was focus on providing an excellent table of contents. Of course we ...

Read more ...

Stay with the Django CBV defaults!

Tuesday, November 27, 2012 (permalink)

One virtue of Django Class Based Views (CBVs) is that they come with pretty good default settings. The virtue of this is you can really pare your code down in size and complexity.

For example, here is an implementation of CBVs based on a straight-forward Django model , stuffage.models.Stuff ...

Read more ...

Case Study: URL Design for petcheatsheets.com

Wednesday, November 21, 2012 (permalink)

Backstory: On Saturday, November 17, 2012 Audrey Roy and I decided to participate in the Petcentric hackathon, a Los Angeles area Pet-themed product/coding contest held at Amplify. We arrived a bit late, but armed with Audrey's idea of creating a pet based reference sheet for owners, pet sitters ...

Read more ...

Django GetOrCreateView

Tuesday, October 16, 2012 (permalink)

Today I decided to use the Django class based view (CBV) CreateView, but I wanted to avoid duplications and submit to the view from the front page of a site. The reason was I needed a simple newsletter signup form. This is what I cooked up and should work for ...

Read more ...

Installing Pycairo on Mountain Lion

Tuesday, September 04, 2012 (permalink)

Pycairo is the binding for the cairo graphics library. It's also not something you can get running with a simple pip install py2cairo. After many hours of working the search engines and dancing to the configure/make/make install melody, I figured out an answer that worked for me ...

Read more ...

Python dictionary as a class

Saturday, September 01, 2012 (permalink)

A long time ago, circa 1999, when I was working in a certain procedural language I found a library that added objects to the language. It did so by playing interesting tricks with key/value structures, which in Python are called dictionaries. In 2005, as a new Python user, I ...

Read more ...

Attaching custom exceptions to functions and classes

Thursday, August 02, 2012 (permalink)

Having too many custom exceptions on a project can be a pain, but a few choices ones are really nice. The problem is that in complex libraries having to import both functions and exceptions becomes a drag. To mitigate having to remember to import custom exceptions, this is a handy ...

Read more ...

Django Update View without slug in the url

Saturday, July 28, 2012 (permalink)

Today I wanted to use the Django Class Based View (CBV) UpdateView but without a slug identifier in the URL. For example, instead of /profiles/pydanny/ I would go to /my-crazy-profile/. Also, I needed to force authentication.

I've done this with Django functional views a few times times, but ...

Read more ...

Simple HTTP Basic Auth Wall

Monday, July 09, 2012 (permalink)

I have a client who wanted their entire unlaunched public content site quickly but temporarily blocked for a short period of time. He wanted a universal password so he could send the site to reviewers, done quickly, and nothing else. In a few days the site will launch, and even ...

Read more ...

Django Class Based View: email form with CAPTCHA

Wednesday, May 23, 2012 (permalink)

Yesterday I showed how to implement a simple email form for Django using Class Based Views. Today I'm going to extend yesterday's work to use the excellent RECAPTCHA service to help reduce spam content.

This version requires pip installing the following into your virtualenv.

  • pip install django-crispy-forms so ...
Read more ...

Simple Django email form using CBV

Tuesday, May 22, 2012 (permalink)

Here's a simple FormView Class Based Views for Django. Here is a sample of how to do one as a simple email form. There is no CAPTCHA in this example, that's the topic of a future blog post.

This version requires the following packages pip installed into your ...

Read more ...

Parsing MongoDB URI

Monday, February 20, 2012 (permalink)

Rather than hard-code the configuration into a Python based settings file, when using a PaaS such as Heroku you want to pick up the MongoDB URI from the system settings. Here's what I do:

# get the dynamic elements from the MongoURI
import os
import re
r = r'^mongodb\:\/\/(?P ...
Read more ...