在具有 Angular 前端的简单 Django Rest Framework 应用程序中会发生什么? [英] What happens in a simple Django Rest Framework application that has an Angular front end?

查看:19
本文介绍了在具有 Angular 前端的简单 Django Rest Framework 应用程序中会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学习 Django 一段时间了,我发现这张图片很有帮助:

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

我现在正在深入研究 Angular JS,并试图弄清楚每个组件(指令、控制器和服务?)如何交互以及是否存在类似的循环".这个博客看起来很接近回答我的问题.

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.

但是,如果我们有一个 Django-Rest-Framework 端点来提供上述示例中的书籍,那么图片有何不同?

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

我们想要 Django 还是 Angular 的 URL 解析?或者更负责任,哪个优先?

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

当我们说 localhost:8000/books 时,事情发生的一般顺序是什么?

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

  • urls.py 能捕捉到这个吗?

urls(r'^books',angular_redirect)

如果是这样,该函数 (angular_redirect) 需要渲染什么才能获得 angular 响应?

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'});

这是否意味着我的控制器然后注册了一个连接到 localhost:8000:/book_list.json 的服务,并且需要在 urls.py 中注册>?

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?

如果我们完全依赖 angular 进行路由,Angular 如何知道从哪里获取 DRF JSON.我见过 this 包,它允许您在 angular 中使用 django 模型JS,但我不确定这是否会使图片或多或少复杂.

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.

推荐答案

您的问题有一个简单的答案:Angular 处理前端,Django(和 DRF)处理后端,其中包括 URL.完全由 Angular 驱动的站点上的用户永远不应该直接访问由 Django 提供的 URL,除非初始页面提供页面结构和 JS 本身.

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.

除此之外,两者之间唯一的交互是当 Angular 通过 Ajax 调用专门从 Django 请求 JSON 时.这很可能与用户的导航事件有关,但同样可以按时间触发或通过某种 websocket 功能触发.

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.

这篇关于在具有 Angular 前端的简单 Django Rest Framework 应用程序中会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆