在 Django 中使用 AngularJS 的最佳实践是什么 [英] What are the best practices to use AngularJS with Django

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

问题描述

我即将开始一个项目,客户端使用 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 的模板语言之间的相似性,这里几乎不值得付出努力.对于大多数此类项目,我会将 AngularJS 的静态服务与 Django REST Framework 配对.

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 Framework URL(尽可能严格地编写),然后是一个通用模式,该模式将其他所有内容指向我的基本 AngularJS 应用程序模板我的 STATIC_ROOT 目录用于本地测试/开发场景:

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 和路由的历史黑客方法.如果您只打算使用主题标签,则最终网址匹配可能会更严格(例如,将 /(网址根)与 r'^$' 匹配).

By pointing all of the unmatched requests to the same app/template, you can start using the history-hack method of URLs and routing if you'd prefer that to hashtags. If you plan only to stick to hashtags, your final URL match could be stricter (for example matching / (URL root) with r'^$').

在生产中,我将使用反向代理或像 nginx 这样的慢速客户端 HTTP 服务器来提供 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 Framework 进行通信,我更喜欢使用类似类的 JS 对象来将数据编组到 AngularJS 应用程序和 Django REST 框架中,或者从 AngularJS 应用程序和 Django REST 框架中编组数据.为此,我使用 angular-django-rest-resource 来生成代表 Django 模型类的类我正在 REST Framework 视图中公开.

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.

为了最大限度地提高 angular-django-rest-resource 可以对资源进行的查询的灵活性,我将为 REST 框架安装 django-filter 后端,如这里.这允许 JS 资源请求受参数约束的 Django 对象(例如 /polls/?author=345&finished=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 操作(例如,如果您在不同的 Internet 域上为 HTML 使用第三方 CDN),那么重要的是允许对这些资源的跨域请求.为此,我推荐 django-cors-headers 中间件.

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.

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

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