Skip to content

Instantly share code, notes, and snippets.

@jdriscoll
Created November 24, 2009 14:17
Show Gist options
  • Save jdriscoll/241890 to your computer and use it in GitHub Desktop.
Save jdriscoll/241890 to your computer and use it in GitHub Desktop.
from django.contrib.sites.models import Site
from django.core.urlresolvers import set_script_prefix
class ScriptPrefixMiddleware(object):
""" Sets up url resolvers to return absolute urls
"""
def process_request(self, request):
set_script_prefix("http://%s" % Site.objects.get_current().domain)
return None
# Django settings template
from os import path
PROJECT_ROOT = path.dirname(path.realpath(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
('John Doe', '[email protected]'),
)
MANAGERS = ADMINS
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'site.db'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Detroit'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = path.join(PROJECT_ROOT, 'assets')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = '/assets/'
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin-media/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'REPLACE_ME'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.transaction.TransactionMiddleware',
)
ROOT_URLCONF = 'urls'
TEMPLATE_DIRS = (
path.join(PROJECT_ROOT, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
)
# Default Django URL config
from django.conf.urls.defaults import *
from django.contrib import admin
from django.contrib.auth.views import login, logout
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/(.*)', admin.site.root),
url(r'^login/$', login, {'template_name': 'login.html'}, name='login'),
url(r'^logout/$', logout, {'template_name': 'logout.html'}, name='logout')
)
urlpatterns += patterns('',
(r'^assets/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT,
'show_indexes': True}),
)
from django.db.models import Q
from django.http import HttpResponse, Http404
from django.template import loader, Context, RequestContext
from django.views.generic.list_detail import object_list
def render_response(request, template, **kwargs):
""" Custom version of the Django render_to_response shortcut """
mimetype = kwargs.pop('mimetype', None)
if request is not None:
c = RequestContext(request, kwargs)
else:
c = Context(kwargs)
t = loader.get_template(template)
return HttpResponse(t.render(c), mimetype=mimetype)
def search_results(request, queryset, search_fields, params='', **kwargs):
query_string = request.GET.get('q', '')
page = request.GET.get('p', 1)
search_terms = query_string.split()
results = queryset
if search_terms:
query = Q()
for term in search_terms:
field_query = Q()
for field in search_fields:
field_query |= Q(**{field + '__icontains': term})
query &= field_query
results = results.filter(query).distinct()
extra_context = kwargs.pop('extra_context', {})
extra_context['query_string'] = query_string
extra_context['page'] = page
extra_context['params'] = params
extra_context['result_count'] = results.count()
return object_list(request, results, page=page,
extra_context=extra_context, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment