Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django serve static index.html with view at '/' url

I have my index.html in /static/ folder. My django app is running ok when i try:

http://127.0.0.1:8000/index.html

But i want to acces index.html by url:

http://127.0.0.1:8000/

I wrote a view and it works:

class IndexView(TemplateView):
    template_name = 'index.html'

I also added to urls.py(this lets me serve static like http://127.0.0.1:8000/css/style.css):

url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve', {
            'document_root': settings.STATIC_ROOT, 'show_indexes':True
        }),

But i think there is a way to do what i want without TemplateView.

Any suggestions? Thanks. My django version is: Django 1.5

EDIT:

The reason i placed index.html into static: i want to make Phonegap compatible django app, so after proper coding, all i have to do is --> make .zip from static folder and upload it to Phonegap as mobile app. Easy and clean.

like image 867
Feanor Avatar asked Aug 01 '13 08:08

Feanor


People also ask

How do I serve static files in Django?

Serving the site and your static files from the same serverPush your code up to the deployment server. On the server, run collectstatic to copy all the static files into STATIC_ROOT . Configure your web server to serve the files in STATIC_ROOT under the URL STATIC_URL .

What does Collectstatic do in Django?

collectstatic. Collects the static files into STATIC_ROOT . Duplicate file names are by default resolved in a similar way to how template resolution works: the file that is first found in one of the specified locations will be used. If you're confused, the findstatic command can help show you which files are found.

What is Staticfiles?

What Are Static Files? Static files are files that don't change when your application is running. These files do a lot to improve your application, but they aren't dynamically generated by your Python web server like a usual HTML response.


2 Answers

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:

  1. Run client app sources watcher to rebuild your scripts/styles/templates (brunch watch, grunt job or gulp watch task)
  2. Collect static with django for production
  3. 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'),
)
like image 159
Anton Egorov Avatar answered Oct 26 '22 20:10

Anton Egorov


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"),
]
like image 34
Alasdair Avatar answered Oct 26 '22 19:10

Alasdair