python - webpack 和 django 找不到静态文件

标签 python django web server webpack

问题是我可以在浏览器上访问应用程序,但不能访问静态 Assets (js、jsx 和图像)。

我正在使用的技术:

django-webpack-loader 0.2.4
React 0.14
Django 1.8.5
Python 2.7

Django 静态文件的部分设置:

103 # Static files (CSS, JavaScript, Images)
104 # https://docs.djangoproject.com/en/1.8/howto/static-files/
105 
106 STATIC_URL = '/static/'
107 STATICFILES_DIRS = (
108     os.path.join(BASE_DIR, 'assets'),
109 )
110 
111 WEBPACK_LOADER = {
112     'DEFAULT': {
113         'BUNDLE_DIR_NAME': 'bundles/',
114         'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
115     }
116 }

webpack.config.js 文件:

  4 // Dependencies
  5 var path = require('path')
  6 var webpack = require('webpack')
  7 var BundleTracker = require('webpack-bundle-tracker')
  8 
  9 module.exports = {
 10     // The base directory (absolute path) for resolving the entry option.
 11     context: __dirname,
 12 
 13     entry: './assets/js/index',
 14 
 15     output: {
 16         // Where the compiled bundle to be stored.
 17         path: path.resolve('./assets/bundles/'),
 18         // Naming convention webpack should use.
 19         filename: '[name]-[hash].js'
 20     },
 21 
 22     plugins: [
 23         // Where webpack stores data about bundles.
 24         new BundleTracker({filename: './webpack-stats.json'}),
 25         // Makes jQuery available in every module.
 26         new webpack.ProvidePlugin({
 27             $: 'jquery',
 28             jQuery: 'jquery',
 29             'window.jQuery': 'jquery'
 30         })
 31     ],
 32 
 33     module: {
 34         loaders: [
 35             // A regexp that tells webpack user the following loaders on all
 36             // .js and .jsx files.
 37             {test: /\.jsx?$/,
 38                 exclude: /ndoe_modules/,
 39                 loader: 'babel-loader',
 40                 query: {
 41                     presets: ['react']
 42                 }
 43             },
 44             // use ! to chain loaders
 45             { test: /\.less$/, loader: 'style-loader!css-loader!less-loader' },
 46             {test: /\.css$/, loader: 'style-loader!css-loader'},
 47             // Inline base64 URLs for <=8k images, direct URLs for the rest.
 48             {test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192'}
 49         ]
 50     },
 51 
 52     resolve: {
 53         // Where webpack looks for modules.
 54         modulesDirectories: ['node_modules'],
 55         // Extensions used to resolve modules.
 56         extensions: ['', '.js', '.jsx']
 57     }
 58 }

部分Dockerfile:

  3 COPY start.sh /opt/start.sh
  4 
  5 ADD . /opt/
  6 
  7 RUN /opt/node_modules/webpack/bin/webpack.js --config /opt/webpack.config.js
  8 
  9 RUN chmod +x /opt/start.sh

Django 项目的层次结构:

my_project/
├── Dockerfile
├── api
├── assets
├── my_project
├── db.sqlite3
├── manage.py
├── node_modules
├── package.json
├── requirements.txt
├── static
├── templates
├── webpack-stats.json
└── webpack.config.js

有两台服务器运行 Nginx t01 和 t02。 t01 用于代理,t02 是 Django 项目所在的位置。代理服务器看起来很好,因为 url 在浏览器上有效,只有静态文件找不到(404 错误)。

我在服务器上手动执行静态文件捆绑,因为将生成一个包含绝对路径信息的 webpack-stats.json 文件。

但是,这个项目在我的本地计算机上运行正常。

[编辑]:

我找到了一个解决方案,只需将其添加到 urlpatterns 末尾的 my_project/urls.py

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

最佳答案

在您的 HTML 页面中,您是否加载并呈现了 bundle ? 这应该在您的入口点 Django 模板中。

{% load render_bundle from webpack_loader %}
{% render_bundle 'app' %}

您还需要 publicPath 来匹配您在 Django 中的静态文件设置。在 webpack.config.js 中设置:

output: {
    path: path.resolve('assets/bundles/'),
    publicPath: '/static/bundles/',
    filename: "[name]-[hash].js",
},

关于python - webpack 和 django 找不到静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35920691/

相关文章:

python - imshow()函数不起作用

python - Django , python :AttributeError: 'NoneType' object has no attribute '_meta'

python - 具有 postgres 引用约束的 Django Arrayfield

python - django:如何为工厂男孩动态指定数据库

api - jQuery-旋钮控制Web音频api平移

java - Utdao.save(object) - java.lang.NullPointerException

python - 使用Python 3在Windows中创建虚拟环境

python - 使用 couchdb-python 处理嵌套对象

jquery - 在 Django 中实现 Ajax 搜索

php - Apache HTTP 服务器 (PHP/MySQL) 中的 GIS 应用程序