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