Skip to content

Instantly share code, notes, and snippets.

class YourBaseModel(models.Model):
@property
def child(self):
from django.core.exceptions import ObjectDoesNotExist
for related_object in self._meta.get_all_related_objects():
if not issubclass(related_object.model, self.__class__):
continue
try:
return getattr(self, related_object.get_accessor_name())
{% if condition
%}{% for item in list %}
{{ item }}{%
endfor %}{%
endif %}
{% if user %}
<div class="left_column">
{% endif %}
{% block content %}
{% endblock %}
{% if user %}
</div>
<div class="right_column">
<div class="sidebar_header user_info">
<h2>Welcome.</h2>
import re
re_float = re.compile('[-+]?(\d+\.\d*|\.\d+)$')
re_decimal = re.compile('[-+]?\d+$')
class CompileError(Exception):
pass
@SmileyChris
SmileyChris / __init__.py
Created November 30, 2009 20:39
a Django site_settings app
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.cache import cache
from project.apps.site_settings import models
KEY = 'site-settings-%d' % settings.SITE_ID
def get_settings(from_cache=True):
"""
@SmileyChris
SmileyChris / not_found.py
Created November 29, 2009 09:08
A class which could be used to represent a missing context variable in a Django template
class NotFound(object):
"""
A class representing a context variable which was not found.
TODO: this could be extended to use the ``TEMPLATE_STRING_IF_INVALID``
Django setting for the ``__str__`` and ``__unicode__`` methods.
"""
def __init__(self, name=None):