2

Thing is, I have an angular + cordova app on one module. I just want the REST api from django for the webserver. no views or anything (well only to serve the angular views).

How can I serve the static index.html using django? do I need different project structure? I'm a newbie to django.

Thanks in advance!

This is driving me nuts.

Muli Yulzary
  • 2,559
  • 3
  • 21
  • 39
  • 1
    Is it possible for you to put this index.html page in your server/templates folder? Edit: By the way, for REST api, it's better to not return HTML pages (return JSON objects instead). So just to verify, you want your REST api to serve an html page? – SilentDev Oct 19 '15 at 20:05
  • Yes. the same one I use in my cordova app (index.html) – Muli Yulzary Oct 19 '15 at 20:09

2 Answers2

4

You can use TemplateView directly:

from django.views.generic import TemplateView

...
url(r'^$', TemplateView.as_view(template_name='index.html'))

Remember you need to configure your templates folder to call .html files by name only.

Gocht
  • 9,924
  • 3
  • 42
  • 81
  • Ok new question mate. all of my assets (CSS/JS etc...) are sitting in `app/www...` and django is unable to find them. how do I use staticfiles to overcome this? – Muli Yulzary Oct 19 '15 at 20:15
  • 1
    Here you will find the config you need in that case: [statics files](https://docs.djangoproject.com/en/1.8/ref/settings/#static-files). Note the example using full path. – Gocht Oct 19 '15 at 20:16
  • sadly i tried `STATIC_URL = '/app/www/'` and `STATICFILES_DIRS = ( "/app/www", )` but none worked – Muli Yulzary Oct 19 '15 at 20:25
  • Is that your full path? – Gocht Oct 19 '15 at 20:27
  • I also tried this: `STATIC_URL = 'G:/Desktop/GetSocial/app/www/'`. didn't work. – Muli Yulzary Oct 19 '15 at 20:33
  • 1
    How are you loading static files in template? You should post a new question with more details about this. – Gocht Oct 19 '15 at 20:40
0

First, place this index.html page in your server/templates folder so Django knows about it.

Then, do this in your URLs.py:

from django.views.generic import TemplateView

urlpatterns = [
    url(r'^$', TemplateView.as_view(template_name='index.html')),
]
SilentDev
  • 20,997
  • 28
  • 111
  • 214