创建一个模型父类, 用于存储id和时间
- abstract = True : 不能单独作为一个模型, 是作为一个父模型出现的
from datetime import datetime
from exts import db
class BaseModel(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
date_time = db.Column(db.DateTime, default=datetime.now)
新闻类型和新闻模板
from apps.models import BaseModel
from exts import db
class NewType(BaseModel):
__tablename__ = 'news_type'
type_name = db.Column(db.String(50), nullable=False)
class News(BaseModel):
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text, nullable=False)
news_type_id = db.Column(db.Integer, db.ForeignKey('news_type.id'))
Api和蓝图的使用
from flask import Blueprint
from flask_restful import Api
news_bp = Blueprint('news', __name__)
api = Api(news_bp)
使用js模拟浏览器请求
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div id="container"></div>
</body>
<script>
$(function () {
url = 'http://127.0.0.1:5000/types';
$.get(url,function (data) {
console.log(data)
})
});
</script>
</html>
- 这里会出现一个跨域请求错误
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ef0784b38d48b826cc7428721d17a9f1.png)