0

I have a Django project that serves an API with django-rest to an Angular SPA. Currently the Angular is served by Django (it's in the static folder). This is really convenient because it makes working on it very easily locally with just one manage.py run server.

On AWS, Django is on an nginx EC2 web server. And all the static files, including angular are on the S3. This works fine for the moment. But I'm sensing that it will soon lead to some difficulties and I would like to separate completely the angular project from the django one (even different github repos).

Therefore my infrastructure would look like that :

  • S3 :
    • Angular App
    • Django static and media files
  • EC2 :
    • Django app

As opposed to current :

  • S3 :
    • Django static files (including Angular App) and media files
  • EC2 :
    • Django app

How can I achieve that ? And how this will change the way I launch my server locally ?

Ambroise Collon
  • 3,839
  • 3
  • 18
  • 37

1 Answers1

1

Splitting the two is quite straight forward. Just move all the angularjs code into a separate directory structure and setup the angularjs package as a normal angularjs package. There is an example on the angularjs page: https://docs.angularjs.org/tutorial/step_00.

You can just open the index.html in you browser and everything should work.

OR (this is what I'm normally doing) you serve the angularjs app using a webserver. I'm working on osx which has apache2 pre-installed.

The only thing you will have to change is the authentication which will no longer work once you are serving the angularjs app from a different url.

You can solve this by moving to Token based authentication (make sure to use https in production).

Some guidance can be found here:

Authorization header in AngularJS not working

but there are many more hints to be found on stack overflow so just search stack overflow if you get stuck.

Community
  • 1
  • 1
Niels van Eldik
  • 211
  • 2
  • 7
  • Thanks for the great answer. I'm already using Token based authentications so it won't be a problem. – Ambroise Collon Nov 11 '15 at 14:42
  • In that case you are all set! Just in case you run into any of these issues, check out this link: http://www.django-rest-framework.org/topics/ajax-csrf-cors/ – Niels van Eldik Nov 11 '15 at 14:48