4

I've been learning Django for some time now, and I found this image helpful:

Django Request-Response cycle

I'm now delving into Angular JS, and I'm trying to figure out how each of the components (Directives, Controllers and Services ?) interacts and if there is a similar 'cycle'. This blog looks like it comes close to answering my question.

But how is the picture different if we have a Django-Rest-Framework end point providing the books in the above example?

Do we want URL resolution from Django, or Angular? Or more answerabley, which takes precedent?

What is the general order that things happen in when we go to say localhost:8000/books ?

  • Does urls.py catch this?

    urls(r'^books',angular_redirect)
    

    If so what does that function (angular_redirect) need to render to get angular to respond?

  • Does angular routing catch this?

     $routeProvider.when('/books', {templateUrl: 'partials/book_partial.html', controller: 'MyBookCtrl'});
    

    So does this mean my controller then registers a service hooked up to say localhost:8000:/book_list.json and does that need to be registered in the urls.py?

How does Angular know where to get the DRF JSON, if we're wholly relying on angular for routing. I've seen this package that lets you use the django models in the angular JS, but I'm not certain if that makes the picture more or less complicated.

Apologies if this is overly broad, I'm very new and trying to get my head round some of the general concepts of these technologies. Any advice on narrowing this question so it's answerable would be appreciated.

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173

1 Answers1

2

Your question has a simple answer: Angular handles the front end and Django (and DRF) handles the back end, and that includes URLs. Users on a fully Angular-powered site should never directly hit a URL that is served by Django, except possibly for the initial page that serves the page structure and JS itself.

Apart from that, the only interaction between the two is when Angular specifically requests JSON from Django, via an Ajax call. That may well be in relation to a navigation event by the user, but just as equally could be triggered on a timed basis or via some kind of websocket functionality.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • So, do we not have a urls.py in the app? If we do what does it contain? – AncientSwordRage Feb 18 '15 at 15:03
  • Of course we do, it's what responds to the Ajax calls from Angular to DRF. – Daniel Roseman Feb 18 '15 at 15:04
  • And that points it at the `index.html` that angular fills with partials? – AncientSwordRage Feb 18 '15 at 15:07
  • I don't understand that question. Angular makes an Ajax request, Django routes it via urls.py to a view handled by DRF, DRF replies with JSON, Angular does whatever it wants with that JSON. – Daniel Roseman Feb 18 '15 at 15:21
  • Say you go to `www.example.com/foo` does django handle this in anyway (via like: `urls(r'^foo',angular_redirect)`, and if so what does that function (`angular_redirect`) need to `render` to get angular to respond? If not, how do you stop Django from intercepting? – AncientSwordRage Feb 18 '15 at 15:35
  • [These](http://stackoverflow.com/q/28245038/1075247) [two](http://stackoverflow.com/q/27665466/1075247) I think are having the same confusion.. – AncientSwordRage Feb 18 '15 at 16:16