简单练习用django写api

  • 创建项目
django-admin startproject myapisite

  • 创建app
python3 manage.py startapp blogapi

  • 运行服务
python manage.py runserver

  • 在settings.py中INSTALLED_APPS里设置时区:
改为Asia/Shanghai # TIME_ZONE = 'UTC' TIME_ZONE = 'Asia/Shanghai'

  • 在settings.py中INSTALLED_APPS里添加:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'blogapi', ]

  • 为app添加url等配置
  1. blogapi下新建url.py
from django.urls import path from . import viewsapp_name = 'blogapi' urlpatterns = [ # /blogapi/ path('', views.index, name='index'), ]

  1. blogapi下views.py添加index方法
from django.shortcuts import render from django.http import HttpResponse# Create your views here.def index(request): return HttpResponse('hello blogapi')

  1. 配置项目的urls.py
from django.contrib import admin from django.urls import path, includeurlpatterns = [ path('admin/', admin.site.urls), path('blogapi/', include('blogapi.urls')) ]

  • 配置数据库连接
  1. 项目settings.py中修改DATABASES
DATABASES = { # 'default': { #'ENGINE': 'django.db.backends.sqlite3', #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), # } 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'python_learn', 'USER': 'root', 'HOST': '127.0.0.1', 'PASSWORD': 'yuanzhiying', 'PORT': 3306, 'OPTIONS': {'charset': 'utf8mb4'}, } }

  1. 可以使用命令为数据库自动生成model
【简单练习用django写api】首先确保已安装PyMySQL和mysqlclient
命令行执行:
python3 manage.py inspectdb

结果:
# This is an auto-generated Django model module. # You'll have to do the following manually to clean this up: #* Rearrange models' order #* Make sure each model has one field with primary_key=True #* Make sure each ForeignKey has `on_delete` set to the desired behavior. #* Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table # Feel free to rename the models, but don't rename db_table values or field names. from django.db import models /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/cursors.py:170: Warning: (3719, "'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.") result = self._query(query)class Article(models.Model): title = models.CharField(max_length=150) info = models.CharField(max_length=1500, blank=True, null=True) author = models.CharField(max_length=100, blank=True, null=True) articledate = models.CharField(max_length=50, blank=True, null=True)class Meta: managed = False db_table = 'article'

  • 创建model
from django.db import models# Create your models here.class Article(models.Model): title = models.CharField(max_length=150) info = models.CharField(max_length=1500, blank=True, null=True) author = models.CharField(max_length=100, blank=True, null=True) articledate = models.CharField(max_length=50, blank=True, null=True)class Meta: managed = False db_table = 'article'def __str__(self): return self.title + self.author

  • 命令创建数据库(暂时先使用默认的sqlite3)
  1. 通过运行 makemigrations 命令,Django 会检测你对模型文件的修改(在这种情况下,你已经取得了新的),并且把修改的部分储存为一次 迁移。
操作全部的model python3 manage.py makemigrations 或者 操作单一model python3 manage.py makemigrations blogapi

  1. 运行 migrate命令,在数据库里创建新定义的模型的数据表
python3 manage.py migrate

    推荐阅读