Python+Django+Bootstrap的web环境搭建学习记录

1.准备环境 python3.7
2.安装django pip3 install django
验证是否安装成功

django-admin --version
2.2.1
3.创建项目,在workspace目录下
django-admin startproject myproject
创建成功后会在workspace下出现一个myproject文件
进入myproject目录
cd myproject
django-admin.py startapp testapp
4.配置settings.py
打开myproject/myproject/settings.py
添加testapp
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'testapp',
]
设置中文(注意有的说要设置为‘zh-cn’,但Lib\site-packages\django\conf\locale目录下没有zh-cn这个文件,所以我们用zh-Hans)
#语言
LANGUAGE_CODE = 'zh-Hans'
#时区
TIME_ZONE = 'Asia/Shanghai'
5.测试django服务
在myproject下,执行
python managy.py runserver 0.0.0.0:8000
...
Django version 2.2.1, using settings 'myproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CTRL-BREAK.
在浏览器输入http://localhost:8000访问成功,即基本服务搭建完成
6.bootstrap 网站模板,及django配置
在workspace/myproject下创建static 文件夹
并从github上下载bootstrap下载地址为
https://github.com/twbs/bootstrap/releases/download/v3.3.1/bootstrap-3.3.1-dist.zip
然后将下载的css,js,fonts文件拷贝到static文件夹下
7.配置静态文件
STATIC_URL = '/static/'
STATICFILES_DIRS = (
BASE_DIR+'/static/',
)
8.在workspace/myproject/testapp/下创建templates文件夹并在下面新建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="http://getbootstrap.com/favicon.ico"&gt;

    <title>Starter Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
 <!-- jquery JavaScript -->
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script&gt;
    <!-- Bootstrap core JavaScript -->
    <script src="/static/js/bootstrap.min.js"></script>
</head>
  <body>

    <div class="container">
<h2>表格</h2>
<p> .table 为任意表格添加基本样式 (只有横向分隔线):</p>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
</tr>
<tr>
<td>2</td>
<td>Debbie</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
</tr>
</tbody>
</table>
</div>

</body>
</html>
9.打开workspace/myproject/testapp/views.py,
def index(request):
return render(request,index.html')
10.打开workspace/myproject/myproject/urls.py配置访问路径
from django.contrib import admin
from django.urls import path
from testapp import views

urlpatterns = [
path('admin/', admin.site.urls),
path(index/', views.index),
]
11.启动项目访问http://localhost:8000/index即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值