Serving static HTML files without extension in Django -
i want serve static html files @ development. want every file.html
served @ /file
.
it's clear in production static files should served via nginx or else.
but how @ development, in elegant way? me amazing index.html
@ /
, also.
what have now:
in urls.py
if settings.debug: def index(request): return render(request, 'index.html') urlpatterns += patterns('', url(r'^$', index)) urlpatterns += static('/', document_root=settings.base_dir+'/static/')
and in settings.py
static_url = '/static/' template_dirs = ( os.path.join(base_dir, 'static/'), )
i write view handles adding .html
, configure urlconf use it.
the view
from django.conf import settings django.contrib.staticfiles.views import serve def serve_html(request, file_name): if not file_name: file_name = 'index' file_path = '{}.html'.format(file_name) # use django staticfiles's serve view. return serve(request, file_path)
the urlconf
url(r'^(?p<file_name>.*)$', serve_html)
it important put urlconf last in urls.py
otherwise, catch all requests , other urlconfs not reached.
Comments
Post a Comment