首页 > 解决方案 > 如何在 django 模型中存储条件语句变量?

问题描述

我正在尝试将manage_quota和存储govt_quota到我的模型choice_filling中,但错误表明未定义上述命名变量。我如何将这些值存储在数据库中?

这是我的views.py(更新)

def seat_info(request):
    if request.method == "POST":
        institute_code = request.POST['institute_code']
        seats = request.POST['seates']
        # s75 = int(seats) * 75 / 100
        s25 = int(seats) * (25 / 100)
        # s25 = int(float(seats) * (25.0 / 100.0))
        print("25% is ", s25)

        mq = request.POST['manage_quota']
        mq15 = int(float(s25) * (15.0 / 100.0))
        print("15% is ", mq15)
        # mq = float(mq) / 15.0
        if float(mq) > 15.0:
            return HttpResponse('not allowed')
        manage_quota = int(s25 * float(mq)/100)
        gov = request.POST['govt_quota']
        gov10 = int(float(s25) * (10.0 / 100.0))
        # gov = float(gov) / 10.0
        print("10% is ", gov10)
        if float(gov) > 10.0:
            return HttpResponse('not allowed')
        govt_quota = int(s25 * float(gov)/100)
        clg_id = request.POST['clg_id']
        fees = request.POST['fees']
        course = request.POST['course']
        s = seat_metrix(clg_id_id=clg_id, code=institute_code, total_seats=seats,
                        course_id=course, manage_quota=manage_quota, govt_quota=govt_quota, fees=fees)
        s.save()
        return redirect('college-dashboard')
    strm = streams.objects.all()
    title = "Seat Information"
    page = "Seat Registration"
    return render(request, 'seat_info.html', {'strm': strm, 'title': title, 'page': page})

而错误回溯(更新)现在我正面临这个错误。

 Internal Server Error: /admission/seat-info
    Traceback (most recent call last):
      File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
        response = get_response(request)
      File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response

        response = self.process_exception_by_middleware(e, request)
      File "C:\Users\Dell\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response

        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "C:\Users\Dell\PycharmProjects\AdmissionSystem\Admission\users\views.py", line 378, in seat_info
        course_id=course, govt_quota=govt_quota, manage_quota=manage_quota, fees=fees)
    UnboundLocalError: local variable 'govt_quota' referenced before assignment

更新:

问题已经解决了!我知道它是脏修复,但它工作正常

这完成了工作

if float(gov) > 10.0:
            return HttpResponse('not allowed')
        govt_quota = int(s25 * float(gov)/100)

我已经更新了我的views.py

标签: pythondjangodjango-views

解决方案


推荐阅读