django模型临时保存数据

时间:2018-02-20 16:20:28

标签: django python-3.x postgresql django-models django-views

我使用postgresql作为数据库,它只是将数据永久保存到数据库中。所以,下面给出的是我的views.py文件

@login_required
def add_comment(request):
    if request.method == 'POST':
        comment = request.POST.get('comment')
        floor_id = request.POST.get('floor_id')
        question_id = request.POST.get('question_id')
        floor_detail = get_object_or_404(FloorDetail, id=floor_id)
        question = get_object_or_404(Question, id=question_id)
        answer, created = Answer.objects.get_or_create(floor_no=floor_detail, question=question)
        comment_obj = Comment.objects.create(answer=answer, comment=comment)
        print(answer, comment_obj)
        data = render(request,'mobile/include/single_comment.html', {'comment':comment_obj})

        if data:
            html=data
        else:
            html=None

        return HttpResponse(html)
    else:
        question_id = request.GET.get('question_id')
        floor_id = request.GET.get('floor_id')
        question = get_object_or_404(Question, id=question_id)
        floor_detail = get_object_or_404(FloorDetail, id=floor_id)
        answer = Answer.objects.filter(floor_no=floor_detail, question=question).first()
        comments = Comment.objects.filter(answer=answer)
        data = render(request,'mobile/include/comment.html', {'comments':comments})

        if data:
            html=data
        else:
            html=None

        return HttpResponse(html)

当我使用print(answer,comment_obj)语句时,它会打印出终端上的两个对象,但是当通过django-admin界面检查数据库时,答案& comment_obj实例未显示,表示数据未反映在那里。 请告诉我如何解决它。

0 个答案:

没有答案