美文网首页
Python 中的 web 常见框架(Django 项目1)

Python 中的 web 常见框架(Django 项目1)

作者: 旦暮何枯 | 来源:发表于2018-03-02 09:28 被阅读0次
  • 大包大揽 Django
  • 力求精简 web.py 和 Tornabo
  • 新生代微框架 Flask 和 Bottle

微框架

优势:

  • 聚焦业务逻辑
  • 学习成本低
  • 灵活性和伸缩性较强

劣势:

  • 很多逻辑需要手动编写
  • 安装大量模块后体积较大

Flask

保持核心简单,易扩展。
不包括数据库抽象层,表单验证等。 可通过添加扩展的方式添加功能

安装

pip install flask

通过导入 flask 模块,来验证安装是否成功。

表单开发

表单标签

用于声明表单范围,位于表单标签中的元素将被提交
语法: `<form></form>
属性: method, Enctype, action

表单域

表单域包含文本框,密码框等多种类型
语法: <input.../>
属性: type,name,value

表单域种类

文本框: <...type:text>
密码框:<...type:password>
文本区域:<...type:textarea>
文件上传框:<...type:file>
单选框:<...type:radio>
复选框:<...type:checkbox>


复选框的值为数组形式。

表单按钮

提交按钮
复位按钮
一般按钮

表单提交方式

GET
POST 数据放置在 HTML Header 中提交。

GET 可以被浏览器缓存
数据暴露 URL
适合:单纯请求数据,不进行其他操作
表单数据较短,不超过 1024 字符
对安全性要求一般

POST URL 可以被缓存,数据不被缓存
请求不便分享,没有长度限制
适合:不仅仅用于请求,需要将数据插入数据库内
表单数据过长
传送的数据不是 ASCII 编码

Djongo

优势:

  • 详细的文档
  • 全套结局啊方案
  • 强大的 URL 路由配置
  • 完善的自主管理后台

劣势:

  • 紧耦合
  • 自带 ORM 不够强大
  • Template 较弱
    [图片上传失败...(image-54d6bb-1519953931235)]

MTV 模型工作模型:

<img src="https://lh3.googleusercontent.com/-GaKxySImq-0/WoJQLmpzNOI/AAAAAAAAAGg/YpuMPv-m1O4SsBXzWqEgJGv5LpM6UK9JwCHMYCw/I/15184896385708.png" width = "400" alt="MySQL"/>

创建虚拟环境 virtualenv

如果你生产或开发环境需同时支持 Python 2 和 Python 3 ,那就需要 virtualenv。我们是从零开始学习 Django,所以可以直接使用 venv。简单来说,venv 模块是 Python 3.3 版本之后,标准库自带的虚拟环境创建和管理工具,在 Python 3 版本是代替 virtualenv。

使用 venv 在当前系统中创建出一个环境,该环境可以跟当前系统互不影响,你可以随意折腾。出现问题,只要删除即可,不会影响到当前系统。另外,有了 virtualenv 虚拟环境之后,我们就可以把那个文件夹整体拷贝了,部署起来方便很多。

步骤:

一. 创建项目目录
创建项目主目录。并进入主目录,创建项目目录

mkdir DjangoWeb
cd DjangoWeb
mkdir DjangoWebFirst

二. 创建虚拟环境,激活虚拟环境
--no-site-packages 表示不安装系统中的第三库
创建虚拟环境。

virtualenv --no-site-packages env_python

三. 使用 pycharm 创建项目
[图片上传失败...(image-ce1d91-1519953931235)]

在 location 填入项目目录所在路径
在 Interpreter 填入新建的虚拟环境
在 Application 填入第一个网站的名字

创建好后,目录类似这样:
[图片上传失败...(image-53318b-1519953931236)]

创建数据库

python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK
(env_python) fuyiweng@fuyiwengdeMacBook-Pro:~/Documents/study/NiuCodeLesson/DjangoWeb/DjangoWebFirst% python3 manage.py makemigrations
Migrations for 'django_demo':
  django_demo/migrations/0001_initial.py
    - Create model People

新建数据库:
在 models.py 中新建一个 People 类,
在终端执行:python3 manage.py makemigrations
创建建表策略

执行表合并:

NiuCodeLesson/DjangoWeb/DjangoWebFirst% python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, django_demo, sessions
Running migrations:
  Applying django_demo.0001_initial... OK

在 views 中创建页面:
在 views 文件中,新建 first_try 类:

from django.shortcuts import render
from django.http import HttpRequest, HttpResponse
from django_demo.models import People
from django.template import Context, Template


# Create your views here.
def first_try(request):
    person = People(name='jack', job='officer')
    html_string = '''
        <html>
            <head>
                <meta charset="utf-8">
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.6/semantic.css" media="screen" title="no title">
                <title>diango</title>
            </head>
            <body>
                <h1 class="ui center aligned icon header">
                    <i class="hand spock icon"></i>
                    Hello, {{ person.name }}
                </h1>
            </body>
        </html>
    '''
    # 将 html 字符转成模板
    t = Template(html_string)
    # 组上下文
    c = Context({'person': person})
    # 渲染模板
    web_page = t.render(c)
    return HttpResponse(web_page)

在 urls 中增加 first_try 的 url:

  1. 引入 first_try from django_demo.views import first_try
  2. 增加路径 urlpatterns 下添加 path(r'first_try/', first_try),

完成。


源码地址:
『NiuCodeLesson/DjangoWeb/DjangoWebFirst/』:
https://github.com/wengfe/NiuCodeLesson/tree/master/DjangoWeb/DjangoWebFirst

博客地址:
『wengfe.win』:http://www.wengfe.win/2018/02/11/2018-02-11/#more

相关文章

网友评论

      本文标题:Python 中的 web 常见框架(Django 项目1)

      本文链接:https://www.haomeiwen.com/subject/ldvoxftx.html