From d972386123f4e9066b912d2e8275f571d714bcd1 Mon Sep 17 00:00:00 2001 From: barney <15270405776@163.com> Date: Thu, 22 Sep 2022 21:10:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E5=9B=BE=E5=92=8Cdrf=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- article/admin.py | 3 +- article/migrations/0001_initial.py | 25 ++++++++ article/models.py | 16 ++++- article/serializers.py | 19 ++++++ article/urls.py | 9 +++ article/views.py | 94 ++++++++++++++++++++++++++++- db.sqlite3 | Bin 0 -> 135168 bytes drf_vue_blog/settings.py | 17 +----- drf_vue_blog/urls.py | 19 +----- 9 files changed, 168 insertions(+), 34 deletions(-) create mode 100644 article/migrations/0001_initial.py create mode 100644 article/serializers.py create mode 100644 article/urls.py diff --git a/article/admin.py b/article/admin.py index 8c38f3f..f16ebcf 100644 --- a/article/admin.py +++ b/article/admin.py @@ -1,3 +1,4 @@ from django.contrib import admin +from article.models import Article -# Register your models here. +admin.site.register(Article) diff --git a/article/migrations/0001_initial.py b/article/migrations/0001_initial.py new file mode 100644 index 0000000..909aba6 --- /dev/null +++ b/article/migrations/0001_initial.py @@ -0,0 +1,25 @@ +# Generated by Django 3.1.3 on 2022-09-22 10:24 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Article', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=100)), + ('body', models.TextField()), + ('created', models.DateTimeField(default=django.utils.timezone.now)), + ('updated', models.DateTimeField(auto_now=True)), + ], + ), + ] diff --git a/article/models.py b/article/models.py index 71a8362..c2e0efa 100644 --- a/article/models.py +++ b/article/models.py @@ -1,3 +1,17 @@ from django.db import models +from django.utils import timezone -# Create your models here. + +# 博客文章 model +class Article(models.Model): + # 标题 + title = models.CharField(max_length=100) + # 正文 + body = models.TextField() + # 创建时间 + created = models.DateTimeField(default=timezone.now) + # 更新时间 + updated = models.DateTimeField(auto_now=True) + + def __str__(self): + return self.title diff --git a/article/serializers.py b/article/serializers.py new file mode 100644 index 0000000..0fe0a23 --- /dev/null +++ b/article/serializers.py @@ -0,0 +1,19 @@ +from rest_framework import serializers +from article.models import Article + +# 返回文章列表或创建一篇文章 +class ArticleListSerializer(serializers.ModelSerializer): + class Meta: + model = Article + fields = [ + 'id', + 'title', + 'created', + 'body', + ] + +# 返回文章详情 +class ArticleDetailSerializer(serializers.ModelSerializer): + class Meta: + model = Article + fields = '__all__' diff --git a/article/urls.py b/article/urls.py new file mode 100644 index 0000000..b244e33 --- /dev/null +++ b/article/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from article import views + +app_name = 'article' + +urlpatterns = [ + path('', views.ArticleList.as_view(), name='list'), + path('/', views.ArticleDetail.as_view(), name='detail') +] diff --git a/article/views.py b/article/views.py index 91ea44a..710faf0 100644 --- a/article/views.py +++ b/article/views.py @@ -1,3 +1,93 @@ -from django.shortcuts import render +from article.models import Article +from article.serializers import ArticleListSerializer, ArticleDetailSerializer +from rest_framework.decorators import api_view +from rest_framework.response import Response +from rest_framework.views import APIView +from django.http import Http404 +from rest_framework import mixins +from rest_framework import generics +from rest_framework import status -# Create your views here. + +@api_view(['GET', 'POST']) +def article_list(request): + # 获取所有文章列表 + if request.method == 'GET': + articles = Article.objects.all() + serializer = ArticleListSerializer(articles, many=True) + return Response(serializer.data) + # 创建新的文章 + elif request.method == 'POST': + serializer = ArticleListSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + +# 和article_list()功能相同 +class ArticleList(generics.ListCreateAPIView): + queryset = Article.objects.all() + serializer_class = ArticleListSerializer + + +# 第一种版本 + +# class ArticleDetail(APIView): +# """文章详情视图""" +# def get_object(self, pk): +# """获取单个文章对象""" +# try: +# # pk代表主键 +# return Article.objects.get(pk=pk) +# except: +# raise Http404 +# +# def get(self, request, pk): +# article = self.get_object(pk) +# serializer = ArticleDetailSerializer(article) +# # 返回Json数据 +# return Response(serializer.data) +# +# def put(self, request, pk): +# article = self.get_object(pk) +# serializer = ArticleDetailSerializer(article, data=request.data) +# # 验证提交的数据是否合法 +# # 不合法就返回400 +# if serializer.is_valid(): +# # 序列化器将持有的数据反序列化后 +# # 保存到数据库 +# serializer.save() +# return Response(serializer.data) +# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) +# +# def delete(self, request, pk): +# article = self.get_object(pk=pk) +# article.delete() +# # 删除成功后返回204 +# return Response(status=status.HTTP_204_NO_CONTENT) + +# 第二种版本 + +# class ArticleDetail(mixins.RetrieveModelMixin, +# mixins.UpdateModelMixin, +# mixins.DestroyModelMixin, +# generics.GenericAPIView): +# """文章详情视图""" +# queryset = Article.objects.all() +# # 序列化类 +# serializer_class = ArticleDetailSerializer +# +# def get(self, request, *args, **kwargs): +# return self.retrieve(request, *args, **kwargs) +# +# def put(self, request, *args, **kwargs): +# return self.update(request, *args, **kwargs) +# +# def delete(self, request, *args, **kwargs): +# return self.destroy(request, *args, **kwargs) + +# ArticleDetail类还可以简化为下面的代码 +class ArticleDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = Article.objects.all() + serializer_class = ArticleDetailSerializer diff --git a/db.sqlite3 b/db.sqlite3 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..93fb8ecf30474e989dc23488cd696bf0767d946c 100644 GIT binary patch literal 135168 zcmeI5e{37qeZcSd>qJZB$(H4dBfF+(D>kc0z8^gPP>?o^O*@Ju*_P$lN*COY(*`Pwyytl8-{k+Rt(L6t?7VmMY9fQIt*wt zpdGOH-W_?z<4IzbtZD2oBcJZQ_kG{@{eIq$yZ0m1wX1V_Q;V!r>t(eWk*Hycrl}_) z5sIP`=-)W{*Z$0+8(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