This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# wondering how dynamic fields could be added to a model... | |
import copy | |
from django.db import models | |
class DynamicModel(models.base.ModelBase): | |
def _prepare(cls): | |
# calling a function or method to return the extra fields to | |
# add to the model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.template.defaultfilters import floatformat | |
from django.contrib.humanize.templatetags.humanize import intcomma | |
from django.utils.encoding import force_unicode | |
def decimal_to_real(value, precision=2): | |
''' | |
Receives a Decimal instance and returns a string formatted as brazilian Real currency: | |
12,234.00. Without the "R$". | |
''' | |
value = floatformat(value, precision) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
An way to add methods to an instance. | |
>>> class Person(object): | |
... def __init__(self, name): | |
... self.name = name | |
... | |
>>> def upper_name(self): | |
... return self.name.upper() | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render_to_response | |
def myview(request): | |
... | |
return render_to_response('template.html', {'foo': ['one', 'two']}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render_to_response | |
from django.template import RequestContext | |
def myview(request): | |
... | |
return render_to_response('template.html', | |
{'foo': ['one', 'two']}, | |
context_instance=RequestContext(request)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.views.generic.simple import direct_to_template | |
def myview(request): | |
... | |
return direct_to_template(request, 'template.html', | |
{'foo': ['one', 'two']}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from settings import * | |
DEBUG = True | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': 'db.sqlite', | |
'USER': '', | |
'PASSWORD': '', | |
'HOST': '', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
from local_settings import * | |
expect ImportError: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DEBUG = True | |
INTERNAL_IPS = ('127.0.0.1',) | |
MIDDLWARE_CLASSES = MIDDLWARE_CLASSES + ('debug_toolbar.middleware.DebugToolbarMiddleware',) | |
INSTALLED_APPS = INSTALLED_APPS + ('debug_toolbar',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
PROJECT_ROOT = os.path.dirname(__file__) | |
try: | |
execfile(os.path.join(PROJECT_ROOT, 'local_settings.py'), globals(), locals()) | |
except IOError: | |
pass |
OlderNewer