Brad's Blog

web dev, django, running, food, or whatever

Help me support St Jude 2016-05-21

If you've known me for a while, you might know that I'm a runner. Since I live in (near) Memphis, TN it's hard not to get excited about the St Jude Memphis Marathon. Surprisingly, this will only be my second marathon, and this year I've decided to run the race as a St. Jude Hero. What's a St. Jude Hero? Well, I've pledged to raise at least $1500 in donations for St. Jude, and ...

Let's convert a Word Doc to HTML 2016-04-29

tl;dr I wrote a python script to convert Word documents to mostly-clean html. Get it at https://github.com/bradmontgomery/word2html. Ah, Microsoft Word... That glorious business-class software used all-around the world. It's perfect for those long, legal documents consisting of nothing but headers, paragraphs, and bulleted lists. All of which we an easily convert into simple HTML, right. Right? File > Save As > Web Page (.htm). Easy as... No wait, was that supposed to be File ...

50 miles 2016-03-11

Saturday, March 5, 2016. I woke up around 4:30am, and we drove out into the middle of nowhere in southern Mississippi. We arrived around 5:30am, and people were already lining up for the 6:00am start. It was 40°F, but the high for the day was supposed to hit the low-mid 70's. This was the Mississippi Trails 50. I'd run my first 50k in mid-January, but this was my first attempt at a 50-miler—a staple ...

Young Coders at PyTennessee 2016 2016-02-08

On February 6, 2016 I had the pleasure of teaching the Young Coders class at PyTennesse. It was an incredibly fun and rewarding experience, and I'm looking forward to doing this again at some point in the future. The class We had ten young coders ranging in ages from 12 to 17, and spent most of Saturday morning quickly plowing through examples of using python as a calculator, exploring data types like integers, floats, strings, and lists. We kicked ...

Ignoring SuspiciousOperation requests for fun and (profit?) 2015-12-16

If you run a Django site, you're probably familiar with those periodic emails due to a SuspiciousOperation exception; it happens when your site receives a request that contains a host header that's now found in the ALLOWED_HOSTS setting. Yes, they're annoying, and while it's good to know when this happens, I've found that there will typically be one or two requests that you get frequently (I'm looking at you, request for /azenv.php from ...

A custom __date lookup for Django 2015-10-18

⚠ Django 1.9 now includes a built-in __date lookup. If possible, you should use that instead of the code below, which doesn't support timezones. In my post last week on date lookups, I ended with a promise to take a look at building a custom django lookup (namely, a __date lookup). Django includes a basic Lookup class, and to build your own lookup expressions, all you really need to do is: Subclass django.db.models.Lookup define a ...

Date lookups in Django 2015-10-10

A while ago I tweeted out something that I've wanted to see in Django for a very long time, yet have never really taken the time to investigate or implement it: I wish #django had this: M.objects.filter(datetimefield__date=http://t.co/MVFXsN4Ivk(2015, 6, 29)) Has that ever been attempted?— Brad Montgomery (@bkmontgomery) June 29, 2015 Django's ORM has a very rich set of field lookups, but at present, it doesn't support an exact ...

Django Admin Filters from ArrayFields 2015-09-30

I've written before about the cool ArrayField support in Django, and this is another such post. In this one, we'll take a look and see how to turn your model's ArrayField values into filters in the admin. To start out, let's assume we have a model that contains a simple title (a CharField) and some keywords (an ArrayField). It might look something like this: from django.db import models class Item(models.Model): title = models.CharField ...

Disabling the Forms in Django Rest Framework's Browsable API 2015-09-03

If you're building a RESTful api using django, then you're probably aware of Django Rest Framework. It's a great project that will do a lot of the heavy lifting for you. It's also got this really really nice featur: the browsable api. The browsable api gives you out-of-the box access to view your api, and even to interact with it using some auto-generated forms. This is great during development, because you can quickly see exaclty how ...

Webucator: zip, map, and lambda 2015-08-30

Recently, the folks from Webucator, who have a series of python tutorials got in touch with me about one of my blog posts: Python's zip, map, and lambda. They wanted to turn that article into a tutorial video. I thought that sounded like a pretty cool idea, so if you're into video tutorials, give this a view. And check out some of their other tutorials as well.