Нет описания

yangck 46c448d5df Merge branch 'master' of git.linkerplus.com:linker/django_demo 7 лет назад
.idea f8f420d8d1 CROS 7 лет назад
api ef5680441b README.md 7 лет назад
todo f8f420d8d1 CROS 7 лет назад
.gitignore 7b0f33145c migrate 001 7 лет назад
README.md 6b3d4f2b4d update README.md 7 лет назад
db.sqlite3 ef5680441b README.md 7 лет назад
manage.py 1aaa979a5a init commit 7 лет назад
requirements.txt f8f420d8d1 CROS 7 лет назад

README.md

Django toturial

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

Model Doc

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

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

from django.http import HttpResponse
def index(request):
    return HttpResponse("hello django")

Mapping URL Doc

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

Create admin user

python manage.py createsuperuser

Register a model

admin.site.register(Task)   #admin.py

Django REST framework

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

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

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

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