首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法到达静态文件django-react

无法到达静态文件django-react
EN

Stack Overflow用户
提问于 2020-08-06 05:12:31
回答 1查看 776关注 0票数 0

我正在尝试部署一个Django--对弹性豆柄作出反应,部署显示为成功,但只给了我一个白色的空页面,(api调用正常工作)。

检查/var/log/nginx/error.log显示:

代码语言:javascript
复制
2020/08/06 02:58:06 [error] 4461#0: *11 open() "/var/app/current/staticbundle.js" failed (2: No such file or directory)

因此,我理解它试图找到bundle.js,但在错误的路径上,我不知道如何修复,应该如下所示:

代码语言:javascript
复制
/var/app/current/static/bundle.js

我以为它没有正确地构建静态文件夹,但是它确实构建了,结果是这样的:

代码语言:javascript
复制
current
   
│
└───static
│   │   bundle.js
│   │   index.html
│   
└───...

这是我的项目设置

  1. Django侧:

1.1 settings.py

代码语言:javascript
复制
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

1.2 urls.py

代码语言:javascript
复制
from django.contrib import admin
from django.urls import path, include, re_path
from django.views.generic import TemplateView

urlpatterns = [
    path('api/', include('backend.urls')),
    re_path(r'^.*', TemplateView.as_view(template_name='index.html')),

]

1.3 django.config

代码语言:javascript
复制
    option_settings:
      aws:elasticbeanstalk:container:python:
        WSGIPath: cerbero.wsgi
    
      aws:elasticbeanstalk:environment:proxy:staticfiles:
        /static/: static/
    container_commands:
      01_node_install:
        command: "curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash - && sudo yum -y install nodejs"
        ignoreErrors: false
    
      02_npm_install:
        command: "npm install"
        ignoreErrors: false
    
      03_react_collect:
        command: "npm run build"
        ignoreErrors: false

2.WebPack方:- webpack.config.js

代码语言:javascript
复制
// webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {

  entry: './frontend/src/index.js',
  watch : false,
  devtool : 'source-map',
  output : {
    filename: 'bundle.js',
    path : path.resolve(__dirname, 'static/'),
    publicPath: '/',
  },

  devServer: {
    contentBase: './',
    inline: true,
    hot: true,
  },

  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  },
  plugins: [
      new HtmlWebpackPlugin({
        template: './frontend/templates/frontend/index.html',
        filename : './index.html',
        minify: false
    })
  ]
};

  • index.html

代码语言:javascript
复制
<!doctype html>

{%load static %}

<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Cerbero</title>
  </head>
  <body>
     <div id="app"></div>
    <script src = "{% static '/bundle.js' %}"></script>

  </body>
</html>

我真的不知道这个问题是属于姜戈,webpack,还是反应

EN

回答 1

Stack Overflow用户

发布于 2020-08-06 08:19:19

代码语言:javascript
复制
urlpatterns = [
    path('api/', include('backend.urls')),
    re_path(r'^.*', TemplateView.as_view(template_name='index.html')),

]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63277227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档