56π
You can serve static/index.html
for development like this:
if settings.DEBUG:
urlpatterns += url(
r'^$', 'django.contrib.staticfiles.views.serve', kwargs={
'path': 'index.html', 'document_root': settings.STATIC_ROOT}),
But for production you should configure your nginx
(or other frontend server) to serve index.html
file for /
location
UPDATE
I want to explain the case you should do like this. For example your django app is only admin and api view, but client interacts with a single page app (Ember, Angular, whatever). So you project has at least two subprojects, one with your main django app and the second is a client app with all html/js/css stuff. It is very convenient to have client scripts separate from django backend, it allows your frontend developers to do their job and avoid django existence (someday it can be moved to the distinct repo).
So in this case you get the following build workflow:
- Run client app sources watcher to rebuild your scripts/styles/templates (
brunch watch
,grunt
job orgulp
watch task) - Collect static with django for production
- Make sure you have urlpatterns fix for developments and right nginx config for production
Here is my urls.py
example
urlpatterns += patterns(
'django.contrib.staticfiles.views',
url(r'^(?:index.html)?$', 'serve', kwargs={'path': 'index.html'}),
url(r'^(?P<path>(?:js|css|img)/.*)$', 'serve'),
)
45π
You donβt need to subclass TemplateView
in this case. You can use TemplateView
directly in your url conf, as long as index.html
is in your templates directory.
from django.views.generic.base import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='index.html'), name="home"),
]
- [Django]-How should I write tests for Forms in Django?
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
- [Django]-Using IntellijIdea within an existing virtualenv
4π
Check out my long explanation of how to serve index.html on /
in this answer (or extended as a blog post). That solution alone might not be sufficient, however, if you want to have a full-fledged SPA served by Django (because you need frontend routing).
Iβve been playing with different methods for routing /static/
to /
, forwarding all requests to the frontend, finding index.html files. In the end I found the best method to solve all of this was not by tweaking urls.py, but as an extension of WhiteNoise that I released as django-spa (installation instructions in the README).
You can find some of the related discussion in this WhiteNoise issue.
- [Django]-Django: return string from view
- [Django]-Unique BooleanField value in Django?
- [Django]-What is a NoReverseMatch error, and how do I fix it?
4π
Just wrap your static HTML file in an iframe defined in a templated HTML file. With some style tweaks can make the iframe 100% width and height.
{% load static %}
<html>
<head>
<title>Templated HTML</title>
<style>
html, body {
width: 100%;
width: 100%;
margin: 0;
padding: 0;
border-width: 0;
}
iframe {
position: absolute;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
margin: 0;
padding: 0;
border-width: 0;
}
</style>
</head>
<body>
{{ content }}
<iframe src="{% static 'main/html/test.html' %}"></iframe>
</body>
</html>
- [Django]-Write only, read only fields in django rest framework
- [Django]-Why does django run everything twice?
- [Django]-Paginate relationship in Django REST Framework?
3π
you can create templates directory, put the html there and then render it from views.py
def index(request):
return render(request, 'my_app/index.html', context={})
dont forget to set the templates_dir in the settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES_DIR = os.path.join(BASE_DIR, "templates")
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR,],
'APP_DIRS': True,
...
- [Django]-Django filter JSONField list of dicts
- [Django]-Custom django admin templates not working
- [Django]-Django β No module named _sqlite3