Django和Angular 2 templateUrl

时间:2016-10-23 11:57:04

标签: python django angular django-rest-framework

我决定将现有的Angular 2应用程序集成到我的Django REST项目中。

  1. 使用我的前端的静态文件夹创建Django应用程序:

    urls.py:

    from django.conf.urls import url
    from frontend import views
    from django.conf import settings
    from django.conf.urls.static import static
    
    
    urlpatterns = [
        url(r'^$', views.index,  name='index'),
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    views.py:

    from django.shortcuts import render
    
    def index(request):
        """
        Renders the Angular2 SPA
        """
        return render(request, template_name='index.html')
    
  2. 将我的Angular 2应用移动到前端静态文件夹

  3. 在我的设置中添加静态设置:

    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'frontend', 'static'),
    ]
    
    STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
  4. 在index.html中进行更改:

    <html>
      <head>
        <base href="/">
        <title>PhotoHub</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        {% load staticfiles %}
    
    
        <link rel="stylesheet" href="{% static "node_modules/bootstrap/dist/css/bootstrap.min.css" %}">
        <link href="{% static "bower_components/font-awesome/css/font-awesome.min.css" %}" rel="stylesheet" />
        <link href="{% static "bower_components/alertify.js/themes/alertify.core.css" %}" rel="stylesheet" />
        <link href="{% static "bower_components/alertify.js/themes/alertify.bootstrap.css" %}" rel="stylesheet" />
        <link rel="stylesheet" href="{% static "styles.css" %}">
    
        <script src="{% static "bower_components/jquery/dist/jquery.min.js" %}"></script>
        <script src="{% static "node_modules/bootstrap/dist/js/bootstrap.min.js" %}"></script>
        <script src="{% static "bower_components/alertify.js/lib/alertify.min.js" %}"></script>
        <!-- 1. Load libraries -->
         <!-- Polyfill(s) for older browsers -->
        <script src="{% static "node_modules/core-js/client/shim.min.js" %}"></script>
        <script src="{% static "node_modules/zone.js/dist/zone.js" %}"></script>
        <script src="{% static "node_modules/reflect-metadata/Reflect.js" %}"></script>
        <script src="{% static "node_modules/systemjs/dist/system.src.js" %}"></script>
        <!-- 2. Configure SystemJS -->
        <script src="{% static "systemjs.config.js" %}"></script>
        <script>
          System.import('app').catch(function(err){ console.error(err); });
        </script>
      </head>
      <!-- 3. Display the application -->
      <body>
        <photohub></photohub>
      </body>
    </html>
    
  5. systemjs.config.js中进行更改:

    (function (global) {
      System.config({
        //static
        defaultJSExtensions: true,
        paths: {
          // paths serve as alias
          'npm:': 'static/node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
          app: 'static/dist',
          // angular bundles
          '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
          '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
          '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
          '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
          '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
          '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
          '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
          '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
          // other libraries
          'rxjs':                      'npm:rxjs',
          'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
          app: {
            main: './main.js',
            defaultExtension: 'js'
          },
          rxjs: {
            defaultExtension: 'js'
          },
          'angular-in-memory-web-api': {
            main: './index.js',
            defaultExtension: 'js'
          }
        }
      });
    })(this);
    
  6. TypeScript文件在/dist中编译,模板保留在/app中。当我使用两台不同的服务器时,templateUrl的路径如下所示:

    templateUrl: './app/app.component.html'
    

    现在我试图以这种方式声明templateUrl,但它不起作用:

    templateUrl: '{{ STATIC_URL }}' + '/app/app.component.html'
    

    现在如何处理模板网址?

    响应:

      

    “GET /app/components/login/login.component.html HTTP / 1.1”404 2601
      找不到:/app/app.component.html

    JavaScript文件加载良好:

      

    “GET /app/components/login/login.component.html HTTP / 1.1”404 2601
      找不到:/app/components/register/register.component.html

2 个答案:

答案 0 :(得分:1)

这解决了问题:

Query query = database.child(user.getUid()).orderByValue();
query.addChildEventListener(...

答案 1 :(得分:0)

我不确定您有什么错误,但您可能应该使用static标记在模板中创建静态资产的网址:

{% load static %}

templateUrl: "{% static '/app/app.component.html' %}"

Doc:https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#std:templatetag-static