Last active
December 15, 2015 19:39
-
-
Save blackrobot/5313350 to your computer and use it in GitHub Desktop.
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 | |
# All of your normal settings... | |
# Utility function to join paths | |
def get_path(*args): | |
return os.path.realpath(os.path.join(*args)) | |
# Project Base Path, ie: /var/www/my_project/source/ | |
SOURCE_ROOT = get_path(os.path.dirname(__file__), '..') | |
# HTML design specific settings | |
HTML_DESIGN_ROOT = get_path(SOURCE_ROOT, "templates/html/") | |
HTML_DESIGN_RELATIVE_PATH = "html" | |
HTML_DESIGN_NAMESPACE = "html" |
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 | |
from django.conf import settings | |
from django.conf.urls.defaults import patterns, url | |
from django.contrib.staticfiles.urls import staticfiles_urlpatterns | |
from django.views.static import directory_index | |
from django.shortcuts import redirect | |
from django.views.generic.simple import direct_to_template | |
def html_design_view(*args, **kwargs): | |
""" This will load design templates from the "html" directory in your | |
templates folder. It's only turned on if DEBUG is True. It will also | |
show indexes if you leave off the filename and extension. | |
""" | |
path = kwargs['path'] | |
# The path is a directory | |
if path.endswith('/') or not path: | |
directory = os.path.join(settings.HTML_DESIGN_ROOT, path) | |
return directory_index(path.rstrip('/'), directory) | |
# Missing a trailing slash | |
elif '.' not in path.split('/')[-1]: | |
return redirect('/%s/' % path) | |
# Render the template | |
kwargs['template'] = os.path.join(settings.HTML_DESIGN_RELATIVE_PATH, path) | |
return direct_to_template(*args, **kwargs) | |
def html_design_urlpatterns(): | |
re = r"^%s/(?P<path>.*)$" % settings.HTML_DESIGN_NAMESPACE | |
return patterns('', url(re, html_design_view)) | |
def local_serve_urlpatterns(): | |
re = r"^%s(?P<path>.*)$" % settings.MEDIA_URL.lstrip('/') | |
options = {'document_root': settings.MEDIA_ROOT, 'show_indexes': True} | |
return patterns( | |
'django.views.static', | |
url(re, 'serve', options), | |
) + staticfiles_urlpatterns() |
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.conf import settings | |
from django.conf.urls.defaults import patterns, url, include | |
from django.contrib import admin | |
from source.utils.tools import html_design_urlpatterns, local_serve_urlpatterns | |
admin.autodiscover() | |
urlpatterns = patterns('', | |
# All of your regular urls... | |
) | |
if getattr(settings, 'DEBUG', False): | |
urlpatterns = html_design_urlpatterns() + urlpatterns | |
if getattr(settings, 'LOCAL_SERVE', False): | |
urlpatterns = local_serve_urlpatterns() + urlpatterns |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment