gpt4 book ai didi

python-3.x - asset_url_for() 在 flask cookies 切割器 (flask-webpack) 中返回 None

转载 作者:行者123 更新时间:2023-12-03 03:40:45 24 4
gpt4 key购买 nike

我是 webpack 新手。我正在使用flask-cookie-cutter依次使用 Flask-Webpack
我在该应用程序中遇到问题,无法找到我正在开发的图像。
图片位于 assets/img 文件夹中我尝试过使用 {{ asset_url_for('img/img_name.png') }} 及其各种变体 - 但它只是不返回任何内容。
我尝试了通常的 flask 方式 {{ url_for('static', filename='img/img_name.png') }}
但我似乎无法访问它们。

const path = require('path');
const webpack = require('webpack');

/*
* Webpack Plugins
*/
const ManifestRevisionPlugin = require('manifest-revision-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

// take debug mode from the environment
const debug = (process.env.NODE_ENV !== 'production');

// Development asset host (webpack dev server)
const publicHost = debug ? 'http://0.0.0.0:2992' : '';

const rootAssetPath = path.join(__dirname, 'assets');

module.exports = {
// configuration
context: __dirname,
entry: {
main_js: './assets/js/main',
main_css: [
path.join(__dirname, 'node_modules', 'materialize-css', 'dist', 'css', 'materialize.css'),
path.join(__dirname, 'assets', 'css', 'style.css'),
],
},
output: {
path: path.join(__dirname, 'dsi_website', 'static', 'build'),
publicPath: `${publicHost}/static/build/`,
filename: '[name].[hash].js',
chunkFilename: '[id].[hash].js',
},
resolve: {
extensions: ['.js', '.jsx', '.css'],
},
devtool: 'source-map',
devServer: {
headers: { 'Access-Control-Allow-Origin': '*' },
},
module: {
rules: [
{
test: /\.less$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: debug,
},
},
'css-loader!less-loader',
],
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: debug,
},
},
'css-loader',
],
},
{
test: /\.html$/,
loader: 'raw-loader'
},
{
test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
loader: 'url-loader',
options: {
limit: 10000,
mimetype: 'application/font-woff'
}
},
{
test: /\.(ttf|eot|svg|png|jpe?g|gif|ico)(\?.*)?$/i,
loader: `file-loader?context=${rootAssetPath}&name=[path][name].[hash].[ext]`
},
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['env'],
cacheDirectory: true
}
},
],
},
plugins: [
new MiniCssExtractPlugin({ filename: '[name].[hash].css', }),
new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }),
new ManifestRevisionPlugin(path.join(__dirname, 'dsi_website', 'webpack', 'manifest.json'), {
rootAssetPath,
ignorePaths: ['/js', '/css'],
}),
].concat(debug ? [] : [
// production webpack plugins go here
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
}
}),
]),
};

最佳答案

我遇到了同样的问题 flask-cookie-cutter并通过更新/升级解决它npm并添加extensionsRegex: /\.(jpe?g|png|gif|svg)$/i在下载文件:webpack.config.js,如下所示:

ManifestRevisionPlugin(path.join(__dirname, '<your app>', 'webpack', 'manifest.json'), {
rootAssetPath,
ignorePaths: ['/js', '/css'],
extensionsRegex: /\.(jpe?g|png|gif|svg)$/i
}),

source solution 。正确调用在模板中显示图像:

{{ asset_url_for('img/img_name.png') }}

之后我检查启动屏幕,发现所有图像都正确加载

[WEBPACK]                                    _/assets/img/image_name.20aed528d50316c91ffedb5ba47b8c74.jpg  18.9 KiB            [emitted]

关于python-3.x - asset_url_for() 在 flask cookies 切割器 (flask-webpack) 中返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57273612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com