Skip to content

Instantly share code, notes, and snippets.

Michael Bylstra mbylstra

Block or report user

Report or block mbylstra

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
mbylstra /
Last active Mar 5, 2019
Django scratchpad file. A standalone Django script with the django environment loaded.Place in the root of your Django project. Assumes your is in a folder named 'main'.
import os
import sys
project_dir = os.path.realpath(__file__)
os.environ['DJANGO_SETTINGS_MODULE'] = 'main.settings'
import django
from django.conf import settings
mbylstra / djr_simple_api_view
Last active Dec 19, 2015
If you just want to use Django Rest Framework for authentication and just want to convert a dictionary to json.
View djr_simple_api_view
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 /
Last active Dec 19, 2015
using Django templates for fancy string interpolation
t = Template("The first stooge in the list is {{ stooges.0 }}.")
c = Context({"stooges": ["Larry", "Curly", "Moe"]})
#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 / sort_git_branches_by_last_commit
Last active Dec 19, 2015
sort git branches by last commit
View sort_git_branches_by_last_commit
git for-each-ref --sort=-committerdate refs/heads/
mbylstra / python_source_code_unicode
Last active Dec 19, 2015
allow unicode in python source code
View python_source_code_unicode
# -*- coding: utf-8 -*-
mbylstra / django_runserver_debug_false
Created Jul 11, 2013
If you want to test logging with DEBUG=True on your local Django dev server, you need to do this.
View django_runserver_debug_false runserver --insecure
#modified version off
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 /
Last active Dec 23, 2015
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)
from django.db import connection
mbylstra / disable_mysql_foreign_key_checks
Created Sep 18, 2013
temporarily disable mysql foreign key checks, so you can do something dodgy:
View disable_mysql_foreign_key_checks
SET foreign_key_checks = 0;
DELETE FROM users where id > 45;
SET foreign_key_checks = 1;
mbylstra / create_utf8_mysql_db
Last active Dec 23, 2015
create a utf8 mysql database
View create_utf8_mysql_db
You can’t perform that action at this time.