What happens in a simple Django Rest Framework application that has an Angular front end?

五迷三道 提交于 2019-12-22 10:24:31

问题


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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/28579864/what-happens-in-a-simple-django-rest-framework-application-that-has-an-angular-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!