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">
  • 相关阅读:
    A1047 Student List for Course [unordered_map]
    .net 事务处理的三种方法
    SQline安装
    LeetCode 21 _ 合并两个有序链表
    LeetCode 191 _ 位1的个数
    LeetCode 268 _ 缺失数字
    LeetCode 190 _ 位运算
    LeetCode 136 _ 位运算
    LeetCode 461 _ 位运算
    LeetCode 125 _ 字符串
  • 原文地址:https://www.cnblogs.com/ajanuw/p/14073281.html
Copyright © 2011-2022 走看看