Django/React - Azure 应用服务找不到静态文件
Django/React - Azure App Service can't find static files
我之前已经通过专用服务器部署了我的 Django React 应用程序,现在我正在尝试使用 Azure Web 应用程序功能实现相同的功能,以便我可以更轻松地使用 CI/CD。我已经如下配置了我的项目,但只有我的 django 出现部署,因为我收到“404 main.js 和 index.css 未找到”。
这让我觉得我的静态文件配置有问题,但我不确定。
.yml文件:
name: Build and deploy Python app to Azure Web App - test123
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
working-directory: ./frontend
- name: Set up Python version
uses: actions/setup-python@v1
with:
python-version: '3.8'
- name: Create and start virtual environment
run: |
python -m venv venv
source venv/bin/activate
- name: Install dependencies
run: |
pip install -r requirements.txt
python manage.py collectstatic --noinput
- name: Zip artifact for deployment
run: zip pythonrelease.zip ./* -r
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v2
with:
name: python-app
path: pythonrelease.zip
# Optional: Add step to run tests here (PyTest, Django test suites, etc.)
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v2
with:
name: python-app
path: .
- name: unzip artifact for deployment
run: unzip pythonrelease.zip
- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy@v2
id: deploy-to-webapp
with:
app-name: 'test123'
slot-name: 'Production'
publish-profile: ${{ secrets.secret}}
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
回购结构:
如有任何建议,我们将不胜感激。
干杯
要在您的 Web 应用程序中托管静态文件,请将 whitenoise 包添加到 requirements.txt 并将其配置添加到 settings.py。如此处所述:Django Tips
requirements.txt |白噪声==4.1.2
我之前已经通过专用服务器部署了我的 Django React 应用程序,现在我正在尝试使用 Azure Web 应用程序功能实现相同的功能,以便我可以更轻松地使用 CI/CD。我已经如下配置了我的项目,但只有我的 django 出现部署,因为我收到“404 main.js 和 index.css 未找到”。
这让我觉得我的静态文件配置有问题,但我不确定。
.yml文件:
name: Build and deploy Python app to Azure Web App - test123
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
working-directory: ./frontend
- name: Set up Python version
uses: actions/setup-python@v1
with:
python-version: '3.8'
- name: Create and start virtual environment
run: |
python -m venv venv
source venv/bin/activate
- name: Install dependencies
run: |
pip install -r requirements.txt
python manage.py collectstatic --noinput
- name: Zip artifact for deployment
run: zip pythonrelease.zip ./* -r
- name: Upload artifact for deployment job
uses: actions/upload-artifact@v2
with:
name: python-app
path: pythonrelease.zip
# Optional: Add step to run tests here (PyTest, Django test suites, etc.)
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v2
with:
name: python-app
path: .
- name: unzip artifact for deployment
run: unzip pythonrelease.zip
- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy@v2
id: deploy-to-webapp
with:
app-name: 'test123'
slot-name: 'Production'
publish-profile: ${{ secrets.secret}}
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
回购结构:
如有任何建议,我们将不胜感激。
干杯
要在您的 Web 应用程序中托管静态文件,请将 whitenoise 包添加到 requirements.txt 并将其配置添加到 settings.py。如此处所述:Django Tips
requirements.txt |白噪声==4.1.2