(Django)拆分前端和后端

问题描述 投票:1回答:2

我想拆分前( 早午餐 )和后( Django )。 我有这个文件夹结构:

backend
  mydjangoapp
  static
    mydjangoapp
      image
      javascripts
      stylesheets
      index.html

frontend
  app
  public
    image
    javascripts
    stylesheets
    index.html

例如, index.html中样式表的路径是:

  • [Backend] static/mydjangoapp/stylesheets/app.css
  • [前端] /stylesheets/app.css

我使用前端路径在本地测试前端与早午餐服务器和后端路径与部署django应用程序。 目前部署过程如下所示:

  1. 早午餐
  2. 移动公用文件夹的内容backend/static/mydjangoapp
  3. 更改index.htmlapp.js等内部的所有路径以匹配后端静态路径。

不太方便。 有没有办法自动完成? 我想我可以在后端更改静态路径以匹配前端或编写脚本来执行此操作。 但它不是一个真正合适的解决方案,是吗? 必须有一种方法直接从前端文件夹呈现index.html并加载静态文件而不更改路径。 试图谷歌它,但没有运气。

django brunch
2个回答
0
投票

所以。 我做了一些研究,得出了这个解决方案:

1)我在django应用程序之前使用了nginx服务器,并将其配置为解析静态文件(nginx配置文件,http部分):

server {
    listen <%= ENV["PORT"] %>;
    server_name _;
    keepalive_timeout 5;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }

    location ~* .*\/.+\..+$ {
        root path/to/my/static/files;
    }
}

因此,静态路径与frontend中的相同。 无需改变那些。 Django应用程序根本不解析静态文件。 它只是加载一个模板。

2)我写了一个python脚本来编译和复制所需的文件到后端 (它位于前端根目录):

#!/usr/bin/env python

from subprocess import call
import shutil
import os

BACKEND_FOLDER = "../backend/ui/public"

call(["brunch", "build", "-p"])
shutil.rmtree(BACKEND_FOLDER)
shutil.copytree("public", BACKEND_FOLDER)

print ("Deployed to: " + BACKEND_FOLDER)

现在,我需要做的就是运行该脚本。


0
投票

您可以做的是使用post-brunchafter-brunch插件将文件从frontend/public复制到您选择的django静态目录。

实施例与后早午餐 exports.config = { plugins: { afterBrunch: [ 'cp -r public ../backend/static' // cp -r /from/frontend/path /to/backend/path ] } }

post-brunch exports.config = { plugins: { postBrunch: function(files) { console.log("Run custom javascript code to copy files.") } } }

© www.soinside.com 2019 - 2024. All rights reserved.