Python全系列 教程
3567个小节阅读:5930.4k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
修改question_app\serialiazers.py
模块,添加提交问题序列化器
xxxxxxxxxx
141from rest_framework import serializers
2
3from question_app.models import Question
4
5
6# 创建增加问题序列化器
7class QuestionAddSerializer(serializers.ModelSerializer):
8 title = serializers.CharField(max_length=200)
9 content = serializers.CharField(max_length=1000,allow_blank=True,allow_null=True)
10 section = serializers.IntegerField()
11
12 class Meta:
13 model = Question
14 fields = ['title','content','section']
修改question_app\views.py
模块,添加提交问题视图
xxxxxxxxxx
281from rest_framework.views import APIView
2from rest_framework.response import Response
3
4from question_app.models import Question
5from user_app.models import User
6from course_app.models import Section
7
8class QuestionAddView(APIView):
9 def post(self, request, format=None):
10 # 接收参数
11 serializer = QuestionAddSerializer(data=request.data)
12 # 验证参数
13 if serializer.is_valid():
14 # 获取用户信息
15 data = verify_token(request.META.get("HTTP_AUTHORIZATION"))
16 user = User.objects.filter(phone=data['phone']).first()
17 # 获取课程小节信息
18 section = Section.objects.get(id=serializer.validated_data['section'])
19 # 创建问题
20 Question.objects.create(
21 title= serializer.validated_data['title'],
22 content= serializer.validated_data['content'],
23 section= section,
24 user= user
25 )
26 return Response({'msg':'问题创建成功','code':201})
27 else:
28 return Response({'msg':'问题创建失败','code':400})
修改questionl_app\urls.py
模块,添加提交问题接口路由
xxxxxxxxxx
71from django.urls import path
2
3from . import views
4
5urlpatterns = [
6 path('user_add_question/', views.QuestionAddView.as_view()),
7]