bluesummers
bluesummers

Reputation: 12607

Serving Angular app using Django

I have a DjangoREST-Nginx-Gunicorn build, and I am using angular as the frontend.

This is the first time I work with this stack together, although I'm very familiar with Django. From what I'm used to, I'm serving pages using render with django templating.

Since I am using REST and angular, the project build is kind of different and I wonder how should I serve the angular app through this stack.

This is my project build

├── client
│   ├── client-app
│   └── index.html
├── gunicorn_start.sh
├── run
│   └── gunicorn.sock
└── server
    ├── config
    ├── __init__
    ├── manage.py
    ├── __pycache__
    ├── requirements.txt
    ├── static
    ├── urls.py
    └── views.py

What I wish to do, is to serve client/index.html and let the angular app handle everything from there on.

What would be the right way to do so?

EDIT: I have in views.py

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

But I get an exception

TemplateDoesNotExist at /

Upvotes: 0

Views: 2310

Answers (2)

thebjorn
thebjorn

Reputation: 27321

The template is not being found either because TEMPLATE_DIRS is not set, or because your template isn't inside an app/templates folder, where app is in INSTALLED_APPS (and you have 'django.template.loaders.app_directories.Loader' in TEMPLATE_LOADERS).

ps: if you're not doing anything in your view, i.e. just rendering the template

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

then you can do it in your urls.py:

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

(or maybe you just spelled / wrong? it should be ^$...)

Upvotes: 1

Emad Fani
Emad Fani

Reputation: 445

when domain entered you should redirect the user to client/index.html and then using angular-router you can do pretty much everything you have done using php or django routes for more info about angular routing

Upvotes: 1

Related Questions