我正在使用基于函数的视图在模型中插入新记录,但是问题是某些字段没有保存。
我通过session
获取所有字段,并将它们分配给变量,然后再将它们保存到模型的相应字段。
views.py
request.POST.get(field_name)
models.py
def CreateExam(request):
if request.method == "POST":
st_exam = exam()
topic = request.POST.get("topic")
auth = request.POST.get("author")
date_now = request.POST.get("date_now")
st_exam.topic = request.POST.get("topic")
st_exam.auth = request.POST.get("author")
st_exam.date = date_now
st_exam.total_grade = request.POST.get("total_grade")
st_exam.semester_id = request.POST.get("semester")
st_exam.exam_id = auth[0:2] + topic[0:2] + date_now
# st_exam.course = request.POST.get("course")
y = datetime.now()
st_exam.semester_id = str(y.year) +'-'+request.POST.get("semester")
st_exam.save()
return redirect('/teacher/')
else:
return render(request, '500.html', {"error": "Ooops Illegal access"})
template.html
class exam(models.Model):
exam_id = models.CharField(max_length=40,null=True)
date = models.CharField(max_length=40,null=True)
total_grade = models.FloatField(null=True)
author= models.CharField(max_length=40,null=True)
topic = models.CharField(max_length=40,null=True)
course = models.CharField(max_length=40,null=True)
semester_id = models.CharField(max_length=40,null=True)
我希望所有字段都可以保存,但作者字段不会保存
答案 0 :(得分:0)
将st_exam.auth
更改为st_exam.author
st_exam.topic = topic
st_exam.author = auth
st_exam.date = date_now