Python Django 编写一个简易的后台管理工具4-添加admin模版

导入admin后台模版

  • 可以在网上任意搜索模版,我这里也提供一个地址github
  • 拷贝admin后台的html文件至项目的templates文件夹
  • 创建static文件夹,将admin后台的js,image等文件全部拷贝过来

Django代码修改

  1. setting.py中加入如下代码
STATIC_ROOT = 'static'

# STATIC_ROOT = os.path.join(BASE_DIR,  'static').replace('\','/')

STATICFILES_DIRS  =(
    ("assets", os.path.join(STATIC_ROOT,'assets').replace('\','/')),
    ("bootstrap", os.path.join(STATIC_ROOT,'bootstrap').replace('\','/')),
    ("images", os.path.join(STATIC_ROOT,'images').replace('\','/')),
    ("vendors", os.path.join(STATIC_ROOT,'vendors').replace('\','/')),
)
  1. 修改之前的hello请求,指向index.html
def hello(request):
    return render(request,'index.html')
  1. 启动服务,请求 http://127.0.0.1:8000/hello/ 看到如下结果,证明现在js等资源文件请求不到

  2. 修改页面资源指向, 在资源路径上增加static路径,如下图

  3. 修改完成后,重新请求hello页面,如下图则证明成功

一个简易的admin后台页面就搭建起来了。

原文地址:https://www.cnblogs.com/xiaomingtx/p/11446929.html