zoukankan      html  css  js  c++  java
  • py django 渲染前端打包的视图

    前端打包后基本这样

    $ 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')
    ]
    

    /urls.py

    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/才能看到视图

    1. 修改路由
      /urls.py
    from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('www/', include('www.urls')),
        path('admin/', admin.site.urls),
    ]
    
    1. 在index.html中添加<base href="/www">
  • 相关阅读:
    LeetCode 79. 单词搜索
    LeetCode 1143. 最长公共子序列
    LeetCode 55. 跳跃游戏
    LeetCode 48. 旋转图像
    LeetCode 93. 复原 IP 地址
    LeetCode 456. 132模式
    LeetCode 341. 扁平化嵌套列表迭代器
    LeetCode 73. 矩阵置零
    LeetCode 47. 全排列 II
    LeetCode 46. 全排列
  • 原文地址:https://www.cnblogs.com/ajanuw/p/14073281.html
Copyright © 2011-2022 走看看