首页 > 编程语言 > 详细

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

时间:2019-07-17 01:18:20      阅读:117      评论:0      收藏:0      [点我收藏+]
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">;

????<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>;
????<!-- 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即可

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

原文:https://blog.51cto.com/14430849/2420684

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!