问题描述
当我在 http://127.0.0.1:8000/ 时出现此错误
KeyError at /
'资产'
在 settings.py
>已安装的应用
INSTALLED_APPS = [
'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','django.contrib.sites',#own
'rest_framework','rest_framework.authtoken','allauth','allauth.account','allauth.socialaccount','rest_auth','rest_auth.registration','crispy_forms','webpack_loader','users','questions',
Webpack 加载器
WEBPACK_LOADER = {
'DEFAULT': {
'BUNDLE_DIR_NAME': 'dist/','STATS_FILE': os.path.join(BASE_DIR,'frontend','webpack-stats.json'),}
}
在模板文件夹 (index.html)
{% load render_bundle from webpack_loader %}
<!DOCTYPE html>
<html lang="en">
<head>
<Meta charset="UTF-8">
<Meta http-equiv="X-UA-Compatible" content="IE=edge">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>QuestionTime</title>
</head>
<body>
<h1>Vue JS</h1>
<div id="app"></div>
{% render_bundle 'app' %}
</body>
</html>
(旧见更新)
在终端它说这个
asset = assets['assets'][chunk]
关键错误:'资产'
终端(更新)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py",line 127,in import_module
return _bootstrap._gcd_import(name[level:],package,level)
File "<frozen importlib._bootstrap>",line 1030,in _gcd_import
File "<frozen importlib._bootstrap>",line 1007,in _find_and_load
File "<frozen importlib._bootstrap>",line 984,in _find_and_load_unlocked
ModuleNotFoundError: No module named 'webpack_loader'
更新: 卸载了 Django-webpack5-loader,因为它没有达到我希望的效果。
见上文“终端(更新)”
更新 2
现在所有错误都消失了,唯一的问题是,我的 vue 没有显示在浏览器中。
解决方法
错误似乎在 webpack 包中。这个答案应该会有所帮助:Django Webpack Loader: "Assets" KeyError?
,ModuleNotFoundError:没有名为“webpack_loader”的模块(已解决)
如果你已经完成pip install django-webpack-loader==0.7.0
这样做:
先做:
pip 卸载 django-webpack-loader==0.7.0
或
pip 卸载 django-webpack-loader
然后点击y,然后输入
然后:
pip install django-webpack-loader==0.7.0
,为了使用 django-webpack-loader>=1.0.0,您必须确保在 JavaScript 端使用 webpack-bundle-tracker@1.0.0。
https://pypi.org/project/django-webpack-loader/
这肯定对你有用!