简体   繁体   English

在Angular / Django SPA中存储ng-templates的最佳位置是什么?

[英]What is the best place to store ng-templates in Angular/Django SPA

I am using Django and AngularJS to build an SPA. 我正在使用Django和AngularJS来构建SPA。

I have lots of ng-templates like this: 我有很多像这样的ng-templates:

<script type="text/ng-template" id="item.html">
 // content
</script>

Now, all of them are in one base.html file and thus the template code has become messy. 现在,所有这些都在一个base.html文件中,因此模板代码变得混乱。

I want to put each ng-template in its own html file. 我想将每个ng-template放在自己的html文件中。

Here are the options I could think of: 以下是我能想到的选项:

1. Templates folder : Logical place would be templates folder. 1.模板文件夹 :逻辑位置是模板文件夹。 But when I tried to reference templates like templates/ng-templates/item.html, I got 404 Not Found. 但是当我尝试引用模板/模板/ ng-templates / item.html等模板时,我找到了404 Not Found。 Because Django doesn't serve templates, it uses those for rendering. 因为Django不提供模板,所以它使用它们进行渲染。

2. Static directory : Django does serve the templates from static directory. 2.静态目录 :Django确实从静态目录提供模板。 But it has two minuses: 但它有两个缺点:

  • Server side stuff does not work in a separate html file. 服务器端的东西在单独的html文件中不起作用。 I mean, if you want to check whether a user is authenticated, you cannot do this: 我的意思是,如果要检查用户是否经过身份验证,则无法执行此操作:

    {% if user.is_authenticated() %}

    But if all those templates were stored inside one base.html , it would be easily possible. 但如果所有这些模板都存储在一个base.html ,那么很容易实现。 It is clear why. 很清楚为什么。

  • Problems in production. 生产中的问题。 I wanted to serve all my static files from Amazon S3 and configured my settings to do that, which means that when I do {% static 'css/main.css' %} Django will automatically give me a correct url. 我想从Amazon S3提供我的所有静态文件并配置我的设置来执行此操作,这意味着当我执行{% static 'css/main.css' %} Django会自动给我一个正确的URL。 However, that {% static %} is not available inside angular code. 但是, {% static %}在角度代码中不可用。 So, you will have to hardcode the url, like so: 因此,您必须对网址进行硬编码,如下所示:

    $routeProvider .when('/search', { templateUrl: 'static/ng-templates/search.html', controller: 'SearchController', })

But, guess what? 但猜猜怎么了? Django does not serve files from static directories in production, at least in Heroku . Django不提供生产中静态目录中的文件,至少在Heroku And configuring it to do so has been such a pain in the ass. 并且配置它这样做一直是如此痛苦的屁股。

So, I am back to my original code: saving all ng-templates in one base.html . 所以,我回到原始代码: 将所有ng-templates保存在一个base.html

My question is: 我的问题是:

What is the better way to do this? 有什么更好的方法呢?

It is better to place ng-templates with the static files. 最好将ng-templates与静态文件放在一起。 If you need to use django tags inside of ng-templates, I suppose that something gone wrong, because you are trying to mix logic. 如果你需要在ng-templates中使用django标签,我认为出了问题,因为你试图混合逻辑。

So, here we are moving to you problems with static directory: 所以,在这里我们向您提出静态目录的问题:

1) The workaround is to compile templates on the server side (in some view) and to get compiled html with ajax. 1)解决方法是在服务器端编译模板(在某些视图中)并使用ajax编译html。 But, again, is is not a good way and it would be better to think about decomposition. 但是,再次说,这不是一个好方法,最好考虑分解。

2) If you need to dynamicaly load static files, you need to keep static url somewhere in the javascript. 2)如果你需要动态加载静态文件,你需要在javascript中的某个地方保留静态URL。 For example, you can use tag in the django template and make global variable (global variables are bad practice, but sometime they can help) like window.static_url= "/static/" . 例如,您可以在django模板中使用tag并使全局变量(全局变量是不好的做法,但有时它们可​​以提供帮助),例如window.static_url= "/static/"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM