Django的,REST和角路线 [英] Django, REST and Angular Routes

查看:164
本文介绍了Django的,REST和角路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图环绕结合像Django的AngularJS一个客户端框架,我的头。有一件事,真的困惑我的路线和REST的问题。

I'm trying to wrap my head around combining a client-side framework like AngularJS with Django. One thing that's really confusing me is the issue of routes and REST.

我一直试图在网上看了很多关于它,但文件是有限的,尤其是在Django方面正与角(小片段在这里或那里)相结合。我明白我需要添加像TastyPie一个REST框架,使我的应用程序一个强大的REST接口,以便角插上,抢资源。

I've been trying to read a lot about it online, but documentation is limited, especially in terms of Django being combined with Angular (little snippets here or there). I understand that I need to add a REST framework like TastyPie to make a robust REST interface in my app in order for Angular to plug in and grab resources.

不过,我有角(2)路线插入困惑,如何正确地以这样的方式:(1)我的服务器端应用程序可以使我的单页应用(SPA)我的路线图指出,应该从服务器(Django的)加载信息/渲染模板(角)和检索数据不冲突。就像如果我有某人会在我的网站和做site.com/user/1234 - 这条路线与两个角路线和Django的路由关联 - 除了一个呈现一个模板和其他吐出JSON基于什么是从中检索数据库/服务器。

However, I'm confused as to how to properly map out my routes in such a way that (1) my server-side app can render my single-page app (SPA) with angular plugged in (2) routes that are supposed to load information/render templates (angular) and retrieve data from the server (django) don't conflict. Like if I have someone going on my website and doing site.com/user/1234 - that route is associated with both the Angular route and the Django route - except one renders a template and the other spits out JSON based on what is retrieved from the DB/server.

此外,通过使用REST API,我放弃了很多我有优势,有ModelForms正与我的模型同步等方面?有什么办法用AngularJS维护这个还是我把目光投向了AngularJS替代品。

In addition, by using the REST api, do I forego a lot of the advantages I have in terms of having ModelForms being synchronized with my Models, etc? Is there any way to maintain this with AngularJS or do I have to look towards an AngularJS substitute.

推荐答案

现在的问题是没有真正具体到Django的 - 只是一个理解的SPA后端和前端之间的关系问题。

The question isn't really specific to Django - just a matter of understanding the relationship between back-end and front-end in an SPA.

路由不会后端和前端之间重复。你的Django路线应设置如下:

Routes are not duplicated between the back-end and the front-end. Your Django routes should be set up like:

/api/foo
/api/bar
...

和,提供了一个的的页面完整的HTML谐音的,例如一个的的路线

and one single route that delivers a single page full of HTML partials, e.g.

/

路由的其余部分将在角度​​来定义的,例如

The rest of the routes will be defined in Angular, e.g.

/articles/234
/blog/date/slug
...

这是处理这些面向公众的路线角控制器将反过来使$ HTTP调用对API网址,每个将提供一个角部分。因此没有重复,没有重叠。

The Angular controllers that handle those public-facing routes will in turn make $http calls against the API URLs and each will deliver one Angular partial. So there is no duplication, no overlap.

要你问题的第二部分,你仍然可以构建自己的API数据时使用Django的ORM模型的关系,但是,是的,你会建立前端时丢失所有Django的善良。

To the second part of your question, you can still use the Django ORM model relationships when constructing your API data, but yes, you'll lose all of that Django goodness when building the front-end.

如果你建立你的API的权利,你在每个视图需要将全部数据present的JSON提要,在该视图角消耗。所以,您使用的是后端数据建设ORM,但是你不能只是决定穿越不首先preparing后端数据为它提供数据模板的模型关系。

If you build your API right, all of the data you need in each view will be fully present in the JSON feed that Angular consumes in that view. So you're using the ORM for back-end data construction, but you can't just decide to traverse a model relationship in a template without first preparing the back-end data to provide data for it.

是的,这是事物的思维方式非常不同,是的这是相当多的比做直的Django(或Rails)的更复杂。这是构建Web应用程序,而不是一个网站的成本。

Yes, it's a very different way of thinking of things, and yes it's quite a bit more complex than doing straight Django (or Rails). That's the cost of building a web app rather than a web site.

这篇关于Django的,REST和角路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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