## Django toturial * [Document](https://docs.djangoproject.com/en/1.11/) * [Official toturial](https://docs.djangoproject.com/en/1.11/intro/tutorial01/) ### Create a virtual environment mkvirtualenv todo or python –m venv todo ### Activate the environment (Linux and OS X) source todo/bin/activate ### Activate the environment (Windows) todo\Scripts\activate ### Install Django to the active environment pip install django==1.10 ### Create a new project django-admin.py startproject todo ### Create a database python manage.py migrate ### Run python manage.py runserver ### Create a new app mkdir api cd api django-admin startapp task #compare project structure with linker_srv * [Python Packages](https://docs.python.org/2/tutorial/modules.html#packages) ### Model [Doc](https://docs.djangoproject.com/en/1.11/topics/db/models/) from django.db import models class Task(models.Model): """task model""" title = models.CharField(max_length=200) description = models.TextField(blank=True) created = models.DateTimeField(auto_now=True) is_completed = models.BooleanField(default=False) class Meta: db_table = "task" ### Install app via settings INSTALLED_APPS = ( .... 'api.task', ) ### Database [Doc](https://docs.djangoproject.com/en/1.11/topics/migrations/) python manage.py makemigrations task #note: migration files python manage.py migrate #apply to the database ### Check database structure # # rename table name ### Create simple view [Doc](https://docs.djangoproject.com/en/1.11/topics/http/views/) from django.http import HttpResponse def index(request): return HttpResponse("hello django") ### Mapping URL [Doc](https://docs.djangoproject.com/en/1.11/topics/http/urls/) touch urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.index, name='index'), ] ### Include in main urls url(r'', include('api.task.urls',namespace='task')), ## Django admin [Doc](https://docs.djangoproject.com/en/1.11/ref/contrib/admin/) ### Create admin user python manage.py createsuperuser ### Register a model admin.site.register(Task) #admin.py ## [Django REST framework](http://www.django-rest-framework.org/) * [REST](https://zh.wikipedia.org/wiki/REST) * [Official Quickstart](http://www.django-rest-framework.org/tutorial/quickstart/) ### Install pip install djangorestframework ### Settings INSTALLED_APPS = ( ... 'rest_framework', ) REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAdminUser', ], 'PAGE_SIZE': 10 } ### Django REST Framework Bulk [Doc](https://github.com/miki725/django-rest-framework-bulk) pip install djangorestframework-bulk ### Create View from rest_framework_bulk import BulkModelViewSet from api.task.models import Task from api.task.serializers import TaskSerializer class TaskView(BulkModelViewSet): queryset = Task.objects.all() serializer_class = TaskSerializer ### Create serializers.py from api.task.models import * from rest_framework.serializers import ListSerializer, ModelSerializer class TaskSerializer(ModelSerializer): class Meta: model = Task list_serializer_class = ListSerializer fields = '__all__' ### Register URL from rest_framework_bulk.routes import BulkRouter from api.task.views import TaskView router = BulkRouter() router.register(r'task', TaskView,base_name='task') urlpatterns = urlpatterns + router.urls ### Testing ### Switch Database to mysql [Doc](https://docs.djangoproject.com/en/1.11/ref/databases/) pip install PyMySQL==0.7.9 #edit settings import pymysql; pymysql.install_as_MySQLdb() #database configurations 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'task', 'USER': 'root', 'PASSWORD': 'abc.123', 'HOST': 'XXXXXXXXXX', 'PORT': '3306', } #migration ### [CORS](https://github.com/ottoyiu/django-cors-headers/) pip install django-cors-headers==2.1.0 #configuration INSTALLED_APPS = ( ... 'corsheaders', ... ) MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10 ... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ... ] CORS_ORIGIN_ALLOW_ALL = True