什么是使用AngularJS Django的最佳做法 [英] What are the best practices to use AngularJS with Django

查看:162
本文介绍了什么是使用AngularJS Django的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始一个项目的 AngularJS 作为客户端和 Django的作为服务器端。

I am about to start a project with AngularJS for the client side and Django for the server side.

什么是让他们最喜欢的朋友(静态文件,身份验证,部署等)

What are the best practices to make them work like best friends (static files,auth, deployment, etc.)

推荐答案

有从有趣的优化Django模板您的客户端模板供电方式;但是,考虑到Django和AngularJS的模板语言之间的相似之处,这是几乎从来没有值得努力的在这里。我配对的 Django的REST框架为这类大多数项目AngularJS的静电服。

There are ways of powering your client-side templates from Django templates for interesting optimizations; however, given the similarities between Django and AngularJS's template languages, it's almost never worth the effort here. I will pair static serving of AngularJS with the Django REST Framework for most projects of this sort.

我的 urls.py 的操作顺序几乎总是Django的REST框架的URL第一(书面尽可能严格),随后指向一切以通用模式在我的 STATIC_ROOT DIR本地测试的/ dev场景我基地AngularJS应用程序模板:

My urls.py order of operations is almost always the Django REST Framework URLs first (written as strictly as possible), followed by a generic pattern that points everything else to my base AngularJS application template in my STATIC_ROOT dir for local testing/dev scenarios:

if settings.DEBUG:
    urlpatterns += patterns('django.contrib.staticfiles.views',
        url(r'', 'serve', {
            'document_root': settings.STATIC_ROOT, 
            'path': '/base.html'}
        ),
    )

通过指向所有匹配的请求到相同的应用程序/模板,就可以开始使用URL的历史,破解方法和路由如果你preFER,为井号标签。如果您计划只坚持主题标记,您的最终URL匹配可能是更严格(例如匹配 / (URL根)与 R'^ $' )。

在生产中,我将使用反向代理或慢的客户端的HTTP服务器像nginx的服务于AngularJS(静态)的内容,进行代理的REST服务Django的WSGI应用的要求。

In production, I'll use a reverse proxy or slow-clients HTTP server like nginx to serve the AngularJS (static) content, proxying the requests for REST services to the Django WSGI app.

对于Django的REST框架沟通,我preFER有类似的JS对象的数据编组和从AngularJS应用和Django的REST框架。对于这一点,我使用角Django的休息资源来生成重新present类Django的模型类,我露出了REST框架意见。

For communicating with the Django REST Framework, I prefer to have class-like JS objects to marshal the data to and from the AngularJS app and the Django REST Framework. For this, I use angular-django-rest-resource to generate classes that represent the Django model classes I'm exposing in the REST Framework views.

有关查询中的最大的灵活性角Django的休息资源可以使资源,我将拥有的 Django的过滤器的描述的here 。这使得JS资源来请求参数的限制Django的对象(例如 /调查/作者= 345安培;成品= 1 )。

For maximum flexibility in the queries angular-django-rest-resource can make for resources, I will have the django-filter backend installed for the REST Framework as described here. This allows the JS resources to request the Django objects constrained by parameters (e.g. /polls/?author=345&finished=1).

如果你的服务器上的一个单独的域从那里的AngularJS主模板送达部署Django和REST操作(例如,如果您使用的是第三方CDN在不同的互联网域名为HTML) ,那么它允许跨域请求这些资源是非常重要的。对于这一点,我建议 Django的CORS报头中间件。

If you're deploying the Django and REST operations on a separate domain of servers from whence the AngularJS main template is served (for example, if you're using a third-party CDN on a different Internet domain for the HTML), then it's important to allow cross-domain requests to those resources. For this, I recommend the django-cors-headers middleware.

我希望这是很有帮助的。这不是的的最佳实践集,但它是一个已经为我工作。

I hope this is helpful. It's not the best practice set, but it's one that has worked for me.

这篇关于什么是使用AngularJS Django的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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