Reputation: 4084
I want to serve static html files at development. I want every file.html
to be served at /file
.
It's clear that in production static files should be served via Nginx or something else.
But how to do it at development, in an elegant way? It would me amazing with index.html
at /
, also.
What i 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 also in settings.py
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
Upvotes: 0
Views: 414
Reputation: 5574
I would write a view that handles adding the .html
and configure a URLConf to use it.
The view
from django.conf import settings
from 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 is important to put the URLConf last in your urls.py
otherwise, it will catch all requests and your other URLConfs will not be reached.
Upvotes: 1