我一直在尝试将应用程序部署到 pythonanywhere 但页面只是空白,因为main.6f259c1b.js文件抛出错误`
未捕获的 SyntaxError:意外的令牌 <
` 我一直在关注这篇文章https://www.fusionbox.com/blog/detail/create-react-app-and-django/624/和这个https://www.techiediaries.com/create -react-app-django/ 两篇文章都建议创建一个包含以下内容的视图
class FrontendAppView(View):
"""
Serves the compiled frontend entry point (only works if you have run `yarn
run build`).
"""
def get(self, request):
try:
with open(os.path.join(settings.REACT_APP_DIR, 'build', 'index.html')) as f:
return HttpResponse(f.read())
except FileNotFoundError:
logging.exception('Production build of app not found')
return HttpResponse(
"""
This URL is only used when you have built the production
version of the app. Visit http://localhost:3000/ instead, or
run `yarn run build` to test the production version.
""",
status=501,
)
并在应用程序网站/urls.py
urlpatterns = [
url(r'^', FrontendAppView.as_view())
]
这些说明对我不起作用。这与pushState 路由, react-routing相关,我不知道。我的应用程序在localhost:3000的开发服务器中运行良好,它只在pythonanywhere和带有 mod_wsgi 的本地 apache 服务器中看到。这是我的本地 apache 配置(来自 Django 文档):
WSGIScriptAlias / /home/user/projects/myproject/myproject/wsgi.py
WSGIPythonHome /home/user/.virtualenvs/myproject
WSGIPythonPath home/user/projects/myproject/
<Directory /home/user/projects/myproject/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
这是根
DocumentRoot "/srv/http"
我的部分设置
REACT_APP_DIR = os.path.join(BASE_DIR, 'frontend')
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = [
os.path.join(REACT_APP_DIR, 'build', 'static')
]
所有软件都是最新版本。也许这个评论解决了我的问题,我只是不明白。https://github.com/facebookincubator/create-react-app/issues/1812#issuecomment-286511320
我的项目目录结构
├── api_v0
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ ├── models.py
│ ├── __pycache__
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── myproject
│ ├── __init__.py
│ ├── local.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── frontend
│ ├── build
│ ├── node_modules
│ ├── package.json
│ ├── package-lock.json
│ ├── public
│ ├── README.md
│ └── src
├── manage.py
├── requirements.txt
├── static
│ ├── admin
│ ├── css
│ ├── js
│ ├── media
│ └── rest_framework
└── website
├── admin.py
├── apps.py
├── __init__.py
├── management
├── middleware.py
├── migrations
├── models.py
├── __pycache__
├── tests.py
├── urls.py
└── views.py