Refactored some django stuff to avoid duplication