Last active
July 19, 2016 21:03
-
-
Save crccheck/ae9a6453a960f21e86d1f6471a9655de to your computer and use it in GitHub Desktop.
Single file Django project
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
#!/usr/bin/env python | |
""" | |
Usage: ./web.py [command] | |
Arguments: | |
command The manage.py command to run (default: runserver) | |
""" | |
import os | |
import sys | |
from django.conf import settings | |
from django.conf.urls import url | |
from django.http import Http404 | |
from django.shortcuts import render_to_response | |
from django.template import TemplateDoesNotExist | |
BASE_DIR = os.path.dirname(__file__) | |
def return_404(request, **kwargs): | |
raise Http404("Catch random browser requests we don't care about") | |
def static_site_view(request, path): | |
templates = [ | |
os.path.join(path, 'index.html'), | |
path + '.html', | |
] | |
try: | |
return render_to_response(templates) | |
except TemplateDoesNotExist as e: | |
raise Http404('Template does not exist: {}'.format(e)) | |
settings.configure( | |
DEBUG=True, | |
SECRET_KEY='', | |
ROOT_URLCONF=__name__, | |
INSTALLED_APPS=[ | |
'django.contrib.staticfiles', | |
], | |
TEMPLATES=[ | |
{ | |
'BACKEND': 'django.template.backends.django.DjangoTemplates', | |
'DIRS': [os.path.join(BASE_DIR, 'app')], | |
}, | |
], | |
STATIC_URL='/static/', | |
STATICFILES_DIRS=[ | |
os.path.join(BASE_DIR, 'app'), | |
] | |
) | |
urlpatterns = [ | |
url(r'^favicon.ico|robots.txt$', return_404), | |
url(r'^$', static_site_view, kwargs={'path': ''}), | |
url(r'^(?P<path>.*)/$', static_site_view), | |
] | |
if __name__ == '__main__': | |
from django.core.management import execute_from_command_line | |
if len(sys.argv) < 2: | |
sys.argv.append('runserver') | |
execute_from_command_line(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment