某些字段未保存在Django模型中

时间:2019-07-05 08:36:16

标签: html django python-3.x

我正在使用基于函数的视图在模型中插入新记录,但是问题是某些字段没有保存。

我通过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)

我希望所有字段都可以保存,但作者字段不会保存

1 个答案:

答案 0 :(得分:0)

st_exam.auth更改为st_exam.author

    st_exam.topic = topic
    st_exam.author = auth
    st_exam.date = date_now