分享

Django中引入bootstrap的方法...

 行走在理想边缘 2023-02-06 发布于四川

一、下载bootstrap


下载地址:https://v3./getting-started/#download,选择第二个,下载带有源码的bootstrap,只能通过这种方式(django是封闭的)

下载的目录结构:

dist文件是bootstrap的核心文件 

二、创建一个简单Demo项目

1.在根项目下创建一个static目录,再在static下创建一个bootstrap文件夹。

2.并在根项目下创建一个templates目录用于存放html文件。

3.找到setting.py修改STATIC_URL:(输入到该文件的末尾即可,注意符号)

  1. STATIC_URL = '/static/'
  2. STATICFILES_DIRS = ( os.path.join('static'), )
  3. STATIC_ROOT = ''

4.setting.py修改TEMPLATES下的'DIRS'

注意:BASE_DIR是manage.py文件的所在路径

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

5.找到应用下的view.py文件,修改如下:

  1. #视图函数,返回index.html页面
  2. from django.http import HttpResponse
  3. from django.shortcuts import render
  4. def index(request):
  5. return render(request, 'index.html')

6.在跟路由urls.py文件修改如下:

  1. from django.urls import path
  2. from App import views

  3. urlpatterns = [
  4. # 首页
  5. path('user/', views.index, name="index"),
  6. ]

上面已经写好视图函数并且加好路由了,接下来开始放置bootstrap。

三、配置bootstrap

1.打开步骤一下载的文件,找到dist文件夹,将里面的的3个文件夹css、fonts、js复制
    到/static/bootstrap下。

 2.从步骤一下载的bootstrap的压缩文件找到docs/examples/blog/下的index.html,复制到项目路径步骤二新建的/templates/目录下,然后改名为base.html。

3.将<link href="../../dist/css/bootstrap.min.css" rel="stylesheet">
   改为<link href="/static/bootstrap/css/bootstrap.css" rel="stylesheet">

4.将<link href="blog.css" rel="stylesheet">
   改为<link href="/static/bootstrap/css/blog.css" rel="stylesheet">

 5.将<script src="../../dist/js/bootstrap.min.js"></script>
    改为 <script src="/static/bootstrap/js/bootstrap.js"></script>

 

 6.将步骤一下载的/docs/examples/blog/下的blog.css复制到static/bootstrap/css/目录下

7.新建一个index.html,里面只需要写对base.html页面的继承

{% extends 'base.html' %}

 8.运行Django,浏览器打开http://127.0.0.1:8000/user/

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多