直接下載上次寫的rest,你有其它rest專案也可以直接拿來用
$git clone https://github.com/montanoh/python-djangorestframework-example.git securityRest $cd securityRest/ $./manage.py syncdb $./manage.py runserver此時 http://127.0.0.1:8000/api/, http://127.0.0.1:8000/api/records/ 應該可以正常使用
編輯 myRest/settings.py
因為輸入了中文註解所以在開頭加上 # -*- encoding: utf-8 -*-
在 MIDDLEWARE_CLASSES = ( ... ) 後加上下面這一段,要加上權限管理真正做的事其實只有這一段
REST_FRAMEWORK = {
# 加上這一段 rest api 都會變成唯讀
'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'],
# 加上下面這段,則需要帳號密碼才能存取rest api
'DEFAULT_PERMISSION_CLASSES': ['rest_framework.permissions.IsAdminUser',],
# 其它一般設定
'PAGE_SIZE': 10
}
此時頁面上的api己經不能使用,會出現錯誤訊息,如需存取需用curl,並且需要加上帳號密碼
securityRest$ curl -H 'Accept: application/json; indent=4' -u admin:password123 http://12.0.0.1:8000/api/records/
[
{
"id": 1,
"date": "2016-05-12T07:10:07.954Z",
"gid": "aaa",
"tag": "tag1",
"note": "test",
"money": 100.0
}
]
django 內建管理帳號網址 http://127.0.0.1:8000/admin/ 可以增刪管理帳號
使用指令 manage.py createsuperuser 也可以建立帳號
securityRest$ ./manage.py createsuperuser Username (leave blank to use 'mon'): test Email address: test@test.com Password: Password (again): Superuser created successfully.
沒有留言:
張貼留言
想告訴我什麼都可以留言