django 表单字段widget的参数_python测试开发django10.django连接mysql

前言

Django 对各种数据库提供了很好的支持,包括:PostgreSQL、MySQL、SQLite、Oracle。本篇以mysql为例简单介绍django连接mysql进行数据操作
Django连mysql需要安装驱动mysqlclient

mysqlclient安装

先要安装数据库驱动mysqlclient,使用pip安装就行

pip install mysqlclient

   copying MySQLdb\constants\FLAG.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    copying MySQLdb\constants\REFRESH.py -> build\lib.win-amd64-3.6\MySQLdb\constants
    running build_ext
    building '_mysql' extension
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    ----------------------------------------
Command "e:\python36\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\dell\\AppData\\Local\\Temp\\pip-install-trc0p4gc\\mysqlclient\\setup.py';
  f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record
  C:\Users\dell\AppData\Local\Temp\pip-record-ulwogpct\install-record.txt --single-version-externally-managed --compile" failed with error code 1
  in C:\Users\dell\AppData\Local\Temp\pip-install-trc0p4gc\mysqlclient\

这里我安装的时候出现了报错:“Microsoft Visual C++ 14.0 is required. Get it with “Microsoft Visual C++ Build Tools”: http://landinghub.visualstudio.com/visual-cpp-build-tools” 打开报错给的地址404

解决办法,指定1.3.10版本安装

pip install mysqlclient==1.3.10

C:\Users\dell>pip install mysqlclient==1.3.10
Collecting mysqlclient==1.3.10
  Downloading https://files.pythonhosted.org/packages/c8/e0/e38c1fc71355bbc60e89401674bc0190f39a207f0235bb92b7e7b09948d0/mysqlclient-1.3.10-cp36-cp36m-win_amd64.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 466kB/s
Installing collected packages: mysqlclient
Successfully installed mysqlclient-1.3.10

django配置数据库

settings.py 文件中找到 DATABASES 配置项, django默认连接sqllite。ENGINE:是指连接数据库驱动的名称,有以下几种情况:

  • django.db.backends.postgresql 连接 PostgreSQL

  • django.db.backends.mysql 连接 mysql

  • django.db.backends.sqlite3 连接 sqlite

  • django.db.backends.oracle 连接 oracle

这里我们连接mysql需要账户密码,也就是之前安装mysql的root用户名,和自己设置的密码,NAME是数据库的名称,连接配置如下:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 或者使用 mysql.connector.django
        'NAME': 'test',
        'USER': 'root',
        'PASSWORD': 'yoyo',
        'HOST':'localhost',
        'PORT':'3306',
    }
}

创建表,同步到mysql

类名代表了数据库表名,且继承了models.Model,类里面的字段代表数据表中的字段(name),数据类型则由CharField(相当于varchar)、DateField(相当于datetime), max_length 参数限定长度。

# models.py

from django.db import models

# Create your models here.

class Test(models.Model):
    name = models.CharField(max_length=20)

先创建表结构,在数据库里面新增一些表

python manage.py migrate

D:\web_djo\helloworld>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, hello, 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 admin.0003_logentry_add_action_flag_choices... 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 hello.0001_initial... OK
  Applying hello.0002_auto_20181122_1025... OK
  Applying hello.0003_auto_20181122_1033... OK
  Applying sessions.0001_initial... OK

打开数据库,会发现多了一些表名称,hello_test就是上一步新建的表

75d165e4c899050139b5959676298595.png

接着让 Django 知道我们在我们的模型有一些变更

python manage.py makemigrations hello

D:\web_djo\helloworld>python manage.py makemigrations hello
No changes detected in app 'hello'

再创建hello这个app应用的表结构

python manage.py migrate hello

D:\web_djo\helloworld>python manage.py migrate hello
Operations to perform:
  Apply all migrations: hello
Running migrations:
  No migrations to apply.

操作数据库

在settings.py同一目录新建一个testdb.py文件

# -*- coding: utf-8 -*-

from django.http import HttpResponse

from hello.models import Test

# 数据库操作
def testdb(request):
    test1 = Test(name='yoyo1')
    test1.save()
    return HttpResponse("数据库hello_test添加name成功!看去看看吧")

urls.py配置访问地址

from django.conf.urls import url
from django.urls import re_path, path
from . import view, testdb

urlpatterns = [
    url(r'^testdb$', testdb.testdb),
]

浏览器打开:http://127.0.0.1:8000/testdb 访问一次,数据库里面就会新增一条数据

0b851b6b3a65bfa0b07e26f9b5ff47e1.png

查看数据库hello_test会新增数据

bd421fff502d4b75127bc9a2d9347d99.png

django交流QQ群:779429633

2019年《python全栈自动化测试课程》2月16号开学!

主讲老师:上海-悠悠

上课方式:QQ群视频在线教学

上课时间:每周六、周日晚上20:30-22:30

2019年《python全栈自动化测试课程》2月16号开学!

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值