Ingen beskrivning

ChangkeYang 39b4709d37 README.md 7 år sedan
.idea 39b4709d37 README.md 7 år sedan
api 39b4709d37 README.md 7 år sedan
todo 39b4709d37 README.md 7 år sedan
.gitignore 12f7d90ce0 migrate 001 7 år sedan
README.md 39b4709d37 README.md 7 år sedan
db.sqlite3 39b4709d37 README.md 7 år sedan
manage.py a9326c003e init commit 7 år sedan

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

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 mysqlclient

#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