Skip to content

Instantly share code, notes, and snippets.

def some_view (request):
# Could substitute `None` with some other default value
key = request.POST.get("key", None)
return render(request, "mytemplate.html", {"key": key})
# In mytemplate.html you'll now have `key` available with whatever was submitted via the form. Note
# that there is NO validation here, you should probably be using a form to capture the input,
# validate it, and return it. https://docs.djangoproject.com/en/dev/ref/forms/api/
@joshkehn
joshkehn / gtm_macros.js
Created January 16, 2014 19:09
Google Tag Manager Macros
/*
* Custom JS macros for creating tags in GTM that populate event information from
* element attributes.
*
* Author: Joshua Kehn <josh@byjakt.com>
* Copyright: (c) 2014 by Joshua Kehn
* License: ISC <http://www.isc.org/downloads/software-support-policy/isc-license/>
*/
function () {
@joshkehn
joshkehn / forms.py
Created June 26, 2013 17:35
Ingredient ModelForm (Django)
from django import forms
from menu.models import Ingredient, Diet, FoodPreference
class IngredientForm (forms.ModelForm):
class Meta:
model = Ingredient
exclude = ["franchise"]
def __init__ (self, *args, **kwargs):
brand = kwargs.pop("brand")
@joshkehn
joshkehn / models.py
Created June 26, 2013 17:29
Ingredient and Meal models (Django)
class Ingredient (models.Model):
"""Describes an ingredient for meals. This ingredient contains specific
diet and food preference information and can be attached to multiple meals."""
name = models.CharField(max_length=255)
franchise = models.ForeignKey(Franchise)
diets = models.ManyToManyField(Diet, null=True, blank=True, verbose_name="special diets or food allergies")
preferences = models.ManyToManyField(FoodPreference, null=True, blank=True)
class Meal (NutritionCapable, PricedModel, DatedModel):
"""Basic object representing a meal. NutritionCapable, PricedModel,
{"code":403,"body":"Developer not authorized. The OAuth API is moderated. Did you email api@kout.com with a detailed use case?"}
@joshkehn
joshkehn / reallypull.sh
Created May 15, 2013 20:25
*Really* make sure everything is synced.
alias pull="git pull";
alias push="git push";
alias prod="git push heroku master:master"; # I have a cleaner function for this that pushes the current branch
pull &&
pull &&
pull &&
sleep 10 &&
pull &&
push &&
prod &&
from fabric.api import local
import pip
def freeze ():
local("pip freeze > requirements.txt")
local("git add requirements.txt")
local("git commit -v")
def upgrade ():
for dist in pip.get_installed_distributions():
"""Testing aspen."""
[---]
request.redirect("/test.html")
[---]
You should be redirected to <a href="/test.html">test.html</a>.
@joshkehn
joshkehn / example_mine.py
Last active December 16, 2015 01:59
Example for a bitcoin article I'm writing
__author__ = "Joshua Kehn <josh@kehn.us>"
import sys
looking_for = "123456"
multiple = 7
for i in xrange(int(looking_for), sys.maxint):
s = multiple * i
if str(s).endswith(looking_for):
print "Found multiple: {0}".format(i)
@joshkehn
joshkehn / .gitignore
Last active December 12, 2015 10:29
Quick and Dirty MongoDB profiling
venv
*.pyc