Django 前后端统一部署

2019-05-30

https://cloud.tencent.com/developer/article/1005607

前端代码 build 后放入 Django 项目下

urls.py 添加两行

1
2
3
4
5
from django.views.generic import TemplateView

urlpatterns = [
path('dashboard/', TemplateView.as_view(template_name="index.html")),
]

TEMPLATES 添加 DIRS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['dist'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

添加 STATICFILES_DIRS css/js 静态文件放进去

1
2
3
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "dist/static"),
]

python3 manange.py runserver 后就能访问了