start a project
django 基本命令
1 django-admin startproject xxx
1 python manage.py startapp polls
1 python manage.py createsuperuser
1 2 3 4 rm -f db.sqlite3 rm -r snippets/migrations python manage.py makemigrations snippets python manage.py migrate
msql
windows django2.0
1 pip install mysql-connector-python
1 2 3 4 5 6 7 8 9 10 11 DATABASES = { 'default' : { 'ENGINE' : 'mysql.connector.django' , 'NAME' : 'django_project' , 'USER' : 'lll' , 'PASSWORD' : '123456' , 'HOST' : '127.0.0.1' , 'PORT' : '3306' , } }
windows django3.0
把mysqlclient 更新到最新版,用Linux的配置就可以
1 pip install mysqlclient==1.4.6
linux
1 2 3 4 5 6 7 8 9 10 DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.mysql' , 'NAME' : 'darwin' , 'USER' : 'lll' , 'PASSWORD' : '123456' , 'HOST' : '127.0.0.1' , 'PORT' : '3306' , } }
一些方法
rest framework
1 2 3 pip install djangorestframework pip install markdown # Markdown support for the browsable API. pip install django-filter # Filtering support
web api 框架
1 超链接关系 很好的RESTful设计 -- HyperlinkedModelSerializer
分页器
分页器允许你控制每页返回的数据条数。添加以下配置到 tutorial/settings.py
中使之生效
1 2 3 4 REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 'PAGE_SIZE': 10 }
产生如下效果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 { "count": 2, "next": null, "previous": null, "results": [ { "email": "admin@example.com", "groups": [], "url": "http://127.0.0.1:8000/users/1/", "username": "admin" }, { "email": "tom@example.com", "groups": [ ], "url": "http://127.0.0.1:8000/users/2/", "username": "tom" } ] }
xadmin
替换掉admin的一种选择
Mysql
进入数据库
1 2 3 4 5 6 7 8 # CREATE DATABASE darwin CHARACTER SET utf8; # 并允许wxx用户可以从任意机器上登入mysql的weixx数据库 GRANT ALL PRIVILEGES ON darwin.* TO anadem@"%" IDENTIFIED BY "123456"; # create user 'anadem'@'%' identified by '123456'; grant all privileges on *.* to 'anadem'@'%';
数据库迁移
1 python manage.py dumpdata > data.json
1 python manage.py loaddata data.json
跨域
百度吧,挺多的。