Skip to content

Instantly share code, notes, and snippets.

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():
...
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.
@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:

################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 / 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).
@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 / conf.py
Last active August 29, 2015 13:57
AppSettings
from .conf_base import AppSettings
class Settings(AppSettings):
YOUR_SETTING = True
"""
Give each setting some documentation for sphinx autodoc if you like.
"""
@SmileyChris
SmileyChris / gist:10611277
Last active August 29, 2015 13:59
flatpages template tag
@register.assignment_tag
def flatpages(a, b, *order_first):
"""
Get the flatpages, optionally with specific ones ordered first.
Usage::
{% flatpages '/home/' '/about/' as flatpages %}
{% for flatpage in flatpages %}...{% endfor %}
"""
@SmileyChris
SmileyChris / pipeline_finders.py
Last active November 10, 2015 17:54
Smarter django-pipeline finders
"""
Smarter django-pipeline finders.
In your settings file, you'll want something like this:
# Exclude pipelined sources from being found as static files.
STATICFILES_FINDERS = (
'pipeline_finders.PipelineFileSystemFinder',
'pipeline_finders.PipelineAppDirectoriesFinder',
'pipeline_finders.PipelineFinder',
class UpdateOrCreateMixin(object):
"""
A view mixin to allow views that use SingleObjectMixin to not fail if no pk/slug is provided.
Primarily useful for making an UpdateView that can create objects too. For example::
class MyView(UpdateOrCreateMixin, UpdateView):
...
url(r'^add/$', MyView.as_view(), name='my-add-view'),