Skip to content

Instantly share code, notes, and snippets.

@mbylstra
mbylstra / scratchpad.py
Last active March 5, 2019 23:14
Django scratchpad file. A standalone Django script with the django environment loaded.Place in the root of your Django project. Assumes your settings.py is in a folder named 'main'.
import os
import sys
project_dir = os.path.realpath(__file__)
sys.path.append(project_dir)
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
import django
from django.conf import settings
django.setup()
@mbylstra
mbylstra / djr_simple_api_view
Last active December 19, 2015 06:59
If you just want to use Django Rest Framework for authentication and just want to convert a dictionary to json.
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
class SimpleView(APIView):
renderer_classes = (JSONRenderer,)
permission_classes = (IsAuthenticated,)
def get(self, request):
content = {'testing': 123}
@mbylstra
mbylstra / django_template_string_interp.py
Last active December 19, 2015 08:09
using Django templates for fancy string interpolation
t = Template("The first stooge in the list is {{ stooges.0 }}.")
c = Context({"stooges": ["Larry", "Curly", "Moe"]})
t.render(c)
#or if you want to render from an existing html template
from django.template.loader import render_to_string
rendered = render_to_string('my_template.html', context)
@mbylstra
mbylstra / sort_git_branches_by_last_commit
Last active December 19, 2015 12:19
sort git branches by last commit
git for-each-ref --sort=-committerdate refs/heads/
#http://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit
@mbylstra
mbylstra / python_source_code_unicode
Last active December 19, 2015 12:19
allow unicode in python source code
# -*- coding: utf-8 -*-
@mbylstra
mbylstra / django_runserver_debug_false
Created July 11, 2013 13:32
If you want to test logging with DEBUG=True on your local Django dev server, you need to do this.
manage.py runserver --insecure
#http://stackoverflow.com/questions/5836674/why-does-debug-false-setting-make-my-django-static-files-access-fail
#modified version off http://code.activestate.com/recipes/576602-safe-print/
def sprint(*args, **kwargs):
"""Safely print the given string.
If you want to see the code points for unprintable characters then you
can use `errors="xmlcharrefreplace"`.
"""
errors = kwargs.get('errors', 'replace')
@mbylstra
mbylstra / py_datetime_to_mysql.py
Last active December 23, 2015 04:19
convert a python date to a mysql date string
from django.db import connection
from django.db.backends.mysql.base import DatabaseOperations
def py_datetime_to_mysql(d):
db_ops = DatabaseOperations(connection)
return db_ops.value_to_db_datetime(d)
#eg:
from django.db import connection
py_datetime_to_mysql(datetime.now())
@mbylstra
mbylstra / disable_mysql_foreign_key_checks
Created September 18, 2013 23:44
temporarily disable mysql foreign key checks, so you can do something dodgy:
SET foreign_key_checks = 0;
DELETE FROM users where id > 45;
SET foreign_key_checks = 1;
@mbylstra
mbylstra / create_utf8_mysql_db
Last active December 23, 2015 23:19
create a utf8 mysql database
CREATE DATABASE some_db DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;