Skip to content

Instantly share code, notes, and snippets.

@SmileyChris
SmileyChris / mail.py
Last active March 5, 2016 08:49
Handle template-base email messages in Django
from email.Utils import formataddr
import os
from django.conf import settings
from django.core import mail
from django import template
from django.template.loader import select_template, get_template
def template_email(template_name, extra_context=None, *args, **kwargs):
@SmileyChris
SmileyChris / base_dirs.py
Created February 4, 2013 21:40
This goes in a `settings` package, imported from settings modules with `from .base_dirs import PROJECT_DIR, VAR_ROOT`
"""
Calculates some project directory settings by assuming they are relative this
module's location.
``PROJECT_DIR``
The project module's path.
``VAR_ROOT``
A location to put run-time generated files (such as uploaded files, or the
collation of static files).
################entering some data the first time to my form doesnt work, it gives this error:
IntegrityError at /job/add/article/
(1048, "Column 'author_id' cannot be null")
Request Method: POST
Request URL: http://localhost:8000/job/add/article/
Django Version: 1.4.3
@SmileyChris
SmileyChris / install.rst
Last active November 19, 2019 18:56
Better wrapping of docstring text with Sublime Text 2. Run: patch ~/.config/sublime-text-2/Packages/Default/paragraph.py < paragraph.diff

To install in Sublime Text 3, you'll need to extract the py file from the default package before patching it.

Here are short instructions to do it in Linux. First change to the directory that you have sublime text, unzip the file, and patch it:

cd /opt/sublime_text
unzip Packages/Default.sublime-package paragraph.py -d ~/.config/sublime-text-3/Packages/default
curl https://gist.githubusercontent.com/SmileyChris/4340807/raw/paragraph.diff | patch ~/.config/sublime-text-3/Packages/default/paragraph.py

Or if you've downloaded the diff, you can just patch it from that rather than `curl`ing:

from django import template
from django.utils.safestring import mark_safe
import ttag
register = template.Library()
class Render(ttag.helpers.AsTag):
"""
A block tag that renders its contents to a context variable.
def get_form_kwargs(request):
"""
A simple helper which returns the correct kwargs when a POST request is
used.
Usage::
form = MyForm(**get_form_kwargs(request))
if form.is_valid():
...
import sublime, sublime_plugin
import string
import textwrap
import re
import comment
def previous_line(view, sr):
"""sr should be a Region covering the entire hard line"""
if sr.begin() == 0:
return None
@SmileyChris
SmileyChris / gist:2592044
Created May 4, 2012 04:33
Silent invalid arguments -> None
diff --git a/django/template/base.py b/django/template/base.py
index ab44bfb..3567756 100644
--- a/django/template/base.py
+++ b/django/template/base.py
@@ -551,9 +551,9 @@ class FilterExpression(object):
args = []
constant_arg, var_arg = match.group("constant_arg", "var_arg")
if constant_arg:
- args.append((False, Variable(constant_arg).resolve({})))
+ args.append(mark_safe(Variable(constant_arg).resolve({})))
filters = Q()
for value in your_list:
filters |= Q(your_field=value)
qs = qs.filter(filters)
# Use 4 spaces, not tabs :P
# Don't go overboard with your newlines.
from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
# Do global imports rather than local ones unless you're specifically avoiding
# a circular import.
# Personally, I prefer to import the module rather than the classes.