-1

I just started working with Django today, and am a little lost in the docs at the moment.

I'd like to create a single page application. I'd like the server to return a single index.html for any request that doesn't match "/api/*".

Can somebody show me what the most standard/accepted way to do this with Django would be? I spent a bit of time trying to Google for similar projects, but didn't have any luck so far.

wheresmycookie
  • 683
  • 3
  • 16
  • 39
  • I think you need to check this one: http://stackoverflow.com/questions/21054973/django-url-template-match-everything-except-pattern – Gokhan Sari Jul 02 '16 at 23:29

1 Answers1

1

To "return a single index.html" you need to create a view that renders that index.html. And route the urls you want (all urls not matching /api/ in your case). I'll try to help with an example:

templates/index.html

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <h1>Hello world!</h1>
    </body>
</html>

views.py

from django.shortcuts import render

def index(request):
    return render(request, 'index.html')

urls.py

from django.conf.urls import url
from your_project.your_app import views

urlpatterns = [
    url(r'^api/', views.api, name='api'),
    url(r'^.*$', views.index, name='index'),
]

BTW, if this is the case, you want to render static HTML, I don't think django is the tool you are looking for.

Gokhan Sari
  • 7,185
  • 5
  • 34
  • 32