前端打包后基本这样
$ ls dist
/static index.html
在index.html中的publicPath指向static
1. 创建一个www模块
$ python manage.py startapp www
$ mkdir -p ./www/templates/www
$ mkdir -p ./www/static
2. 将dist的文件拷贝到django项目中
$ cp -a /dist/index.html www/templates/www/
$ cp -a /dist/static/* www/static/
3. 配置视图和路由
www/views.py:
from django.shortcuts import render
def index(request):
return render(request, 'www/index.html')
www/urls.py:
from django.urls import path, include
from . import views
app_name = 'www'
urlpatterns = [
path('', views.index, name='index')
]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('www.urls')),
path('admin/', admin.site.urls),
]
4. 在主模块中导入www
INSTALLED_APPS = [
...
'www.apps.WwwConfig'
]
5. 启动项目
$ python manage.py runserver
启动后在http://127.0.0.1:8000
就能看到视图
将视图放到带有前缀的路由中
如访问http://127.0.0.1:8000/www/
才能看到视图
- 修改路由
/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('www/', include('www.urls')),
path('admin/', admin.site.urls),
]
- 在index.html中添加
<base href="/www">