Category Archives: Uncategorized

Access Elements in a Django JSONField List

I set up my Django project to use MySQL, so that I could use a list in a JSONField. Little did I know that accessing elements of that list by their index would be another problem entirely. There are two confounding things. First, in Jinja2, you can access a for-loop index with the loop.index or loop.index0 keywords, but in a Django project, you need to use forloop.counter or forloop.counter0`. The trailing zero on those keywords specify a zero-indexed counter rather than a one-indexed counter. The second thing is that you need to provide a custom template tag in order to cleanly access elements in a JSONField list by their index.

Continue reading

Using the State Machine Compiler with Python

Today I learned about a state machine compiler and code generator. It provides a small DSL that you can use to describe a state machine and the transitions between the states, which will be compiled to create a number of classes, and then all you have to do is provide the code for the actions. Here, actions, states, and transitions are defined terms that are described in the documentation for the smc tool.

Continue reading

Getting CSRF to work with Rails and Angular

I had trouble posting to my Rails server from an Angular controller until I found this SO post. In /app/controllers/application_controller.rb I added the following code.

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  after_action :set_csrf_cookie_for_ng

  def set_csrf_cookie_for_ng
    cookies['XSRF-TOKEN'] = form_authenticity_token if protect_against_forgery?
  end

  protected

    # In Rails 4.2 and above
    def verified_request?
      super || valid_authenticity_token?(session, request.headers['X-XSRF-TOKEN'])
    end
end

Getting Started with Watercolor

In this post, I’ll discuss options for building a watercolor kit, and provide some introductory reading material.

My Background

I started working in watercolor in 2016. I heard the quote by Annie Dillard, “How we spend our days is, of course, how we spend our lives.” and thought that I should make an effort to carve out more time for art, before I don’t have any time left to carve. I decided to work more in watercolor because, for me, it strikes a balance between range of expression, time, and space. Also, although I’ve done art my whole life, I’ve tended to shy away from color because of it’s difficulty. Focusing on watercolor has allowed/forced me to learn more about color.

Continue reading