DATABASES={
#'default':{
#'ENGINE':'django.db.backends.sqlite3',
#'NAME':os.path.join(BASE_DIR,'db.sqlite3'),
#}
'default':{
'ENGINE':'django.db.backends.mysql',
'USER':'root',
'PASSWORD':'Huada12#$',
'NAME':'django2',
'PORT':3306,
'HOST':'192.168.245.128',
'OPTIONS':{
'init_command':'SETdefault_storage_engine=INNODB',
},
},
}python
報錯:須要安裝mysqlclient
#pip install mysqlclientmysql
#python manage.py migratesql
(venv) D:\PycharmProjects\django2>python manage.py showmigrations
admin
[ ] 0001_initial
[ ] 0002_logentry_remove_auto_add
[ ] 0003_logentry_add_action_flag_choices
auth
[ ] 0001_initial
[ ] 0002_alter_permission_name_max_length
[ ] 0003_alter_user_email_max_length
[ ] 0004_alter_user_username_opts
[ ] 0005_alter_user_last_login_null
[ ] 0006_require_contenttypes_0002
[ ] 0007_alter_validators_add_error_messages
[ ] 0008_alter_user_username_max_length
[ ] 0009_alter_user_last_name_max_length
[ ] 0010_alter_group_name_max_length
[ ] 0011_update_proxy_permissions
contenttypes
[ ] 0001_initial
[ ] 0002_remove_content_type_name
dashboard
(no migrations)
sessions
[ ] 0001_initialshell
怎樣查看
#
sessions
[ ] 0001_initial數據庫
(venv) D:\PycharmProjects\django2>python manage.py sqlmigrate sessions 0001
BEGIN;
--
-- Create model Session
--
CREATE TABLE `django_session` (`session_key` varchar(40) NOT NULL PRIMARY KEY, `session_data` longtext NOT NULL, `expire_date` dat
etime(6) NOT NULL);
CREATE INDEX `django_session_expire_date_a5c62663` ON `django_session` (`expire_date`);
COMMIT;
#django
#
(venv) D:\PycharmProjects\django2>python 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 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 auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying sessions.0001_initial... OK
##session
(venv) D:\PycharmProjects\django2>python manage.py dbshell
CommandError: You appear not to have the 'mysql' program installed or on your path.app
(venv) D:\PycharmProjects\django2>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> exit()ui
(venv) D:\PycharmProjects\django2>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> User.objects.create_user('weizitianming','weizi@163.com','123456')
<User: weizitianming>
>>>spa
(venv) D:\PycharmProjects\django2>python manage.py createsuperuser
(venv) D:\PycharmProjects\django2>python manage.py shell
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='weizitianming')
>>> u
<User: weizitianming>
>>> u.set_password('654321')
>>> u.save()
>>> exit()
(venv) D:\PycharmProjects\django2>
2、GET、POST
GET:
1,返回全部的用戶列表 /users/
2,返回指定用戶的信息 /users/12/
POST
1,修改用戶信息 /users/12/
body:{
username:"xiaoming"
}
PUT
1, 建立一個用戶 /users/
{}
DELETE
1, 刪除一個用戶
3、
User ---》 1000
10
select * from user limit 0,10 第1頁
select * from user limit 11,20 第2頁
select * from user limit 21,30 第3頁
User.objects.all()[0:10] 第1頁
User.objects.all()[10:20] 第2頁
User.objects.all()[20:30] 第2頁
>>> python manage.py shell
>>>from django.contrib.auth.models import User
>>>username='weizitianming'
>>>for I in range(1000):
name = '{}_{}'.format(username,i)
User.objects.create_user(name, '{}.@163.com'.format(username),'123456')
<User: weizitianming_0>
<User: weizitianming_1>
<User: weizitianming_2>
<User: weizitianming_3>
<User: weizitianming_4>
<User: weizitianming_5>
<User: weizitianming_6>
<User: weizitianming_7>
<User: weizitianming_8>
<User: weizitianming_997>
<User: weizitianming_998>
<User: weizitianming_999>
>>> User.objects.all()
<QuerySet [<User: weizitianming>, <User: weizitianming_0>, <User: weizitianming_1>, <User: weizitianming_2>, <User: weizitianming_
3>, <User: weizitianming_4>, <User: weizitianming_5>, <User: weizitianming_6>, <User: weizitianming_7>, <User: weizitianming_8>, <
User: weizitianming_9>, <User: weizitianming_10>, <User: weizitianming_11>, <User: weizitianming_12>, <User: weizitianming_13>, <U
ser: weizitianming_14>, <User: weizitianming_15>, <User: weizitianming_16>, <User: weizitianming_17>, <User: weizitianming_18>, '.
..(remaining elements truncated)...']>
>>>
>>> User.objects.all()[:10]
<QuerySet [<User: weizitianming>, <User: weizitianming_0>, <User: weizitianming_1>, <User: weizitianming_2>, <User: weizitianming_
3>, <User: weizitianming_4>, <User: weizitianming_5>, <User: weizitianming_6>, <User: weizitianming_7>, <User: weizitianming_8>]>
>>> queryset.values()
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'queryset' is not defined
>>> queryset = User.objects.all()[:10]
>>> queryset.values()
<QuerySet [{'id': 1, 'password': 'pbkdf2_sha256$150000$Appx9kEcXmwq$hDzz1VX4MXu6M4ttTIgfaNdXz/2YIgIku19ySeEXtjE=', 'last_login': d
atetime.datetime(2019, 7, 14, 7, 18, 7, 72348, tzinfo=<UTC>), 'is_superuser': False, 'username': 'weizitianming', 'first_name': ''
, 'last_name': '', 'email': 'weizi@163.com', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 7, 14, 4
, 7, 48, 552477, tzinfo=<UTC>)}, {'id': 2, 'password': 'pbkdf2_sha256$150000$o6h9oIwfsjLA$Md9ld8MDvpk8PsHsJsUaKOA06H8bg4KovcSlcqyq
u44=', 'last_login': None, 'is_superuser': False, 'username': 'weizitianming_0', 'first_name': '', 'last_name': '', 'email': '()@1
63.com', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 7, 14, 23, 29, 46, 359455, tzinfo=<UTC>)}, {
'id': 3, 'password': 'pbkdf2_sha256$150000$STqeOGUFBywC$uzvO8eNDahweRPnkvc8y/XD2PTnD0XHxEVRgsGZfMhY=', 'last_login': None, 'is_sup
eruser': False, 'username': 'weizitianming_1', 'first_name': '', 'last_name': '', 'email': '()@163.com', 'is_staff': False, 'is_ac
tive': True, 'date_joined': datetime.datetime(2019, 7, 14, 23, 29, 46, 724075, tzinfo=<UTC>)}, {'id': 4, 'password': 'pbkdf2_sha25
6$150000$PB3xSjKQnfo7$yJ34nisYjMqJe0u/GCNx5pNAvjNUCA9QbyeG8K+/6Ms=', 'last_login': None, 'is_superuser': False, 'username': 'weizi
tianming_2', 'first_name': '', 'last_name': '', 'email': '()@163.com', 'is_staff': False, 'is_active': True, 'date_joined': dateti
me.datetime(2019, 7, 14, 23, 29, 46, 958089, tzinfo=<UTC>)}, {'id': 5, 'password': 'pbkdf2_sha256$150000$AF5GqmmUe5jg$lLFROaAmzi3R
dfxLvs0JGkXB69LauxINsrBUmthimU4=', 'last_login': None, 'is_superuser': False, 'username': 'weizitianming_3', 'first_name': '', 'la
st_name': '', 'email': '()@163.com', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 7, 14, 23, 29, 4
7, 190701, tzinfo=<UTC>)}, {'id': 6, 'password': 'pbkdf2_sha256$150000$yHlHkLbHzcEg$66UqxFVo7np/9bUyiK4dzm2mpj6SJoOTKe5nxSct99s=',
'last_login': None, 'is_superuser': False, 'username': 'weizitianming_4', 'first_name': '', 'last_name': '', 'email': '()@163.com
', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 7, 14, 23, 29, 47, 423714, tzinfo=<UTC>)}, {'id':
7, 'password': 'pbkdf2_sha256$150000$QrbKqdAQje1V$gMUepbUnkvZ0TZZLtnr+1HJDRAGFhRVa7hRszhW+KTg=', 'last_login': None, 'is_superuser
': False, 'username': 'weizitianming_5', 'first_name': '', 'last_name': '', 'email': '()@163.com', 'is_staff': False, 'is_active':
True, 'date_joined': datetime.datetime(2019, 7, 14, 23, 29, 47, 654723, tzinfo=<UTC>)}, {'id': 8, 'password': 'pbkdf2_sha256$1500
00$KtqYIcqWMDzR$1fWS7Nc1ohsAF/HG+LvFWFp1NUQBMYkmwJXW11GsIp0=', 'last_login': None, 'is_superuser': False, 'username': 'weizitianmi
ng_6', 'first_name': '', 'last_name': '', 'email': '()@163.com', 'is_staff': False, 'is_active': True, 'date_joined': datetime.dat
etime(2019, 7, 14, 23, 29, 47, 885736, tzinfo=<UTC>)}, {'id': 9, 'password': 'pbkdf2_sha256$150000$tO2d5l2Z6vK9$Ln1w9ZrGl6DH7DqO35
K9qGt2FrBpwSEbEl1KTgIOddQ=', 'last_login': None, 'is_superuser': False, 'username': 'weizitianming_7', 'first_name': '', 'last_nam
e': '', 'email': '()@163.com', 'is_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 7, 14, 23, 29, 48, 117
749, tzinfo=<UTC>)}, {'id': 10, 'password': 'pbkdf2_sha256$150000$SqrXS5nF4W0v$BuJPVfZ/xxo1E4Z766H1UOCgr58CbU78LYgPjm8lr4A=', 'las
t_login': None, 'is_superuser': False, 'username': 'weizitianming_8', 'first_name': '', 'last_name': '', 'email': '()@163.com', 'i
s_staff': False, 'is_active': True, 'date_joined': datetime.datetime(2019, 7, 14, 23, 29, 48, 350262, tzinfo=<UTC>)}]>
>>>
>>> queryset.values('username','email')
<QuerySet [{'username': 'weizitianming_999', 'email': '()@163.com'}, {'username': 'weizitianming_1000', 'email': 'weizitianming_10
00@163.com'}, {'username': 'weizitianming_1001', 'email': 'weizitianming_1001@163.com'}, {'username': 'weizitianming_1002', 'email
': 'weizitianming_1002@163.com'}, {'username': 'weizitianming_1003', 'email': 'weizitianming_1003@163.com'}, {'username': 'weiziti
anming_1004', 'email': 'weizitianming_1004@163.com'}, {'username': 'weizitianming_1005', 'email': 'weizitianming_1005@163.com'}, {
'username': 'weizitianming_1006', 'email': 'weizitianming_1006@163.com'}, {'username': 'weizitianming_1007', 'email': 'weizitianmi
ng_1007@163.com'}, {'username': 'weizitianming_1008', 'email': 'weizitianming_1008@163.com'}, {'username': 'weizitianming_1009', '
email': 'weizitianming_1009@163.com'}, {'username': 'weizitianming_1010', 'email': 'weizitianming_1010@163.com'}, {'username': 'we
izitianming_1011', 'email': 'weizitianming_1011@163.com'}, {'username': 'weizitianming_1012', 'email': 'weizitianming_1012@163.com
'}, {'username': 'weizitianming_1013', 'email': 'weizitianming_1013@163.com'}, {'username': 'weizitianming_1014', 'email': 'weizit
ianming_1014@163.com'}, {'username': 'weizitianming_1015', 'email': 'weizitianming_1015@163.com'}, {'username': 'weizitianming_101
6', 'email': 'weizitianming_1016@163.com'}, {'username': 'weizitianming_1017', 'email': 'weizitianming_1017@163.com'}, {'username'
: 'weizitianming_1018', 'email': 'weizitianming_1018@163.com'}]>
>>>
>>> list(queryset.values('username','email'))[{'username': 'weizitianming_999', 'email': '()@163.com'}, {'username': 'weizitianming_1000', 'email': 'weizitianming_1000@163.com'}, {'username': 'weizitianming_1001', 'email': 'weizitianming_1001@163.com'}, {'username': 'weizitianming_1002', 'email': 'weizitianming_1002@163.com'}, {'username': 'weizitianming_1003', 'email': 'weizitianming_1003@163.com'}, {'username': 'weizitianming_1004', 'email': 'weizitianming_1004@163.com'}, {'username': 'weizitianming_1005', 'email': 'weizitianming_1005@163.com'}, {'username': 'weizitianming_1006', 'email': 'weizitianming_1006@163.com'}, {'username': 'weizitianming_1007', 'email': 'weizitianming_1007@163.com'}, {'username': 'weizitianming_1008', 'email': 'weizitianming_1008@163.com'}, {'username': 'weizitianming_1009', 'email': 'weizitianming_1009@163.com'}, {'username': 'weizitianming_1010', 'email': 'weizitianming_1010@163.com'}, {'username': 'weizitianming_1011', 'email': 'weizitianming_1011@163.com'}, {'username': 'weizitianming_1012', 'email': 'weizitianming_1012@163.com'}, {'username': 'weizitianming_1013', 'email': 'weizitianming_1013@163.com'}, {'username': 'weizitianming_1014', 'email': 'weizitianming_1014@163.com'}, {'username': 'weizitianming_1015', 'email': 'weizitianming_1015@163.com'}, {'username': 'weizitianming_1016', 'email': 'weizitianming_1016@163.com'}, {'username': 'weizitianming_1017', 'email': 'weizitianming_1017@163.com'}, {'username': 'weizitianming_1018', 'email': 'weizitianming_1018@163.com'}]>>>