编程知识 cdmana.com

Summary of today's Django lessons: data reverse query triggers error field 'score_ jieshoufankui‘ expected a number but got ‘‘-20211109

Database reverse query

I want to use it request.GET.get(‘score_jieshoufankui2’, 100) To avoid error reporting when the value cannot be obtained .

  • views.py
#  Establish the result interface of data reverse query 
@login_required
def data_search_results(request):
    #  Query out user maximal query_set
    user_permission_query = user_permission(request)

    print('request.GET1',request.GET)
    
    #  Receive query conditions 
    if 'submit' in request.GET and request.GET['submit']:
        # request.GET.get() and request.GET() difference 
        print('request.GET2',request.GET)
        score_jieshoufankui1 = request.GET.get('score_jieshoufankui1', 0)
        print(score_jieshoufankui1)
        score_jieshoufankui2 = request.GET.get('score_jieshoufankui2', 100)
        print(score_jieshoufankui2)
        score_xueximinruidu1 = request.GET.get('score_xueximinruidu1', 0)
        score_xueximinruidu2 = request.GET.get('score_xueximinruidu2', 100)
        score_jieguodaoxiang1 = request.GET.get('score_jieguodaoxiang1', 0)
        score_jieguodaoxiang2 = request.GET.get('score_jieguodaoxiang2', 100)
        score_quanjusiwei1 = request.GET.get('score_quanjusiwei1', 0)
        score_quanjusiwei2 = request.GET.get('score_quanjusiwei2', 100)
        score_shiyingli1 = request.GET.get('score_shiyingli1', 0)
        score_shiyingli2 = request.GET.get('score_shiyingli2', 100)
        score_chengjiutaren1 = request.GET.get('score_chengjiutaren1', 0)
        score_chengjiutaren2 = request.GET.get('score_chengjiutaren2', 100)
        score_lingdaoyiyuan1 = request.GET.get('score_lingdaoyiyuan1', 0)
        score_lingdaoyiyuan2 = request.GET.get('score_lingdaoyiyuan2', 100)
        score_pinghengrenjiyurenwu1 = request.GET.get('score_pinghengrenjiyurenwu1', 0)
        score_pinghengrenjiyurenwu2 = request.GET.get('score_pinghengrenjiyurenwu2', 100)
        score_fudao1 = request.GET.get('score_fudao1', 0)
        score_fudao2 = request.GET.get('score_fudao2', 100)
        score_shouquanweize1 = request.GET.get('score_shouquanweize1', 0)
        score_shouquanweize2 = request.GET.get('score_shouquanweize2', 100)
        score_jianlichenggongtuandui1 = request.GET.get('score_jianlichenggongtuandui1', 0)
        score_jianlichenggongtuandui2 = request.GET.get('score_jianlichenggongtuandui2', 100)
        score_guanlirenjiguanxi1 = request.GET.get('score_guanlirenjiguanxi1', 0)
        score_guanlirenjiguanxi2 = request.GET.get('score_guanlirenjiguanxi2', 100)
        score_yingxiangli1 = request.GET.get('score_yingxiangli1', 0)
        score_yingxiangli2 = request.GET.get('score_yingxiangli2', 100)
        score_jianlihuobanguanxi1 = request.GET.get('score_jianlihuobanguanxi1', 0)
        score_jianlihuobanguanxi2 = request.GET.get('score_jianlihuobanguanxi2', 100)
        score_jihuayuzuzhi1 = request.GET.get('score_jihuayuzuzhi1', 0)
        score_jihuayuzuzhi2 = request.GET.get('score_jihuayuzuzhi2', 100)

        #  Create a string for query criteria 
        str_list = Q(score_jieshoufankui__range=(score_jieshoufankui1,score_jieshoufankui2)) 
        print(str_list)
        str_list = str_list & Q(score_xueximinruidu__range=(score_xueximinruidu1,score_xueximinruidu2)) 
        print(str_list)
        str_list = str_list & Q(score_jieguodaoxiang__range=(score_jieguodaoxiang1,score_jieguodaoxiang2)) 
        print(str_list)
        str_list = str_list & Q(score_quanjusiwei__range=(score_quanjusiwei1,score_quanjusiwei2)) 
        str_list = str_list & Q(score_shiyingli__range=(score_shiyingli1,score_shiyingli2)) 
        str_list = str_list & Q(score_chengjiutaren__range=(score_chengjiutaren1,score_chengjiutaren2)) 
        str_list = str_list & Q(score_lingdaoyiyuan__range=(score_lingdaoyiyuan1,score_lingdaoyiyuan2)) 
        str_list = str_list & Q(score_pinghengrenjiyurenwu__range=(score_pinghengrenjiyurenwu1,score_pinghengrenjiyurenwu2)) 
        str_list = str_list & Q(score_fudao__range=(score_fudao1,score_fudao2))
        str_list = str_list & Q(score_shouquanweize__range=(score_shouquanweize1,score_shouquanweize2)) 
        str_list = str_list & Q(score_jianlichenggongtuandui__range=(score_jianlichenggongtuandui1,score_jianlichenggongtuandui2)) 
        str_list = str_list & Q(score_guanlirenjiguanxi__range=(score_guanlirenjiguanxi1,score_guanlirenjiguanxi2)) 
        str_list = str_list & Q(score_yingxiangli__range=(score_yingxiangli1,score_yingxiangli2)) 
        str_list = str_list & Q(score_jianlihuobanguanxi__range=(score_jianlihuobanguanxi1,score_jianlihuobanguanxi2)) 
        str_list = str_list & Q(score_jihuayuzuzhi__range=(score_jihuayuzuzhi1,score_jihuayuzuzhi2)) 


        employee_list = Employee.objects.filter(str_list)  & user_permission_query
        print(str_list)
        return render(request, 'PPDASH/data_search_results.html',context={
    'employee_list': employee_list})
    else:
        return render(request, 'PPDASH/data_search_form.html',context={
    })

Trigger ValueError:Field ‘score_jieshoufankui’ expected a number but got ‘’.

 Insert picture description here

Later print print(‘request.GET2’,request.GET), Found that the value of the following key value pair is an empty list , No wonder it shows ’’.

request.GET2 <QueryDict: {
    'score_jieshoufankui1': [''], 'score_jieshoufankui2': [''], 'score_xueximinruidu1': [''], 'score_xueximinruidu2': [''], 'score_jieguodaoxiang1': [''], 'score_jieguodaoxiang2': [''], 'score_quanjusiwei1': [''], 'score_quanjusiwei2': [''], 'score_shiyingli1': [''], 'score_shiyingli2': [''], 'score_chengjiutaren1': [''], 'score_chengjiutaren2': [''], 'score_lingdaoyiyuan1': [''], 'score_lingdaoyiyuan2': [''], 'score_pinghengrenjiyurenwu1': [''], 'score_pinghengrenjiyurenwu2': [''], 'score_fudao1': [''], 'score_fudao2': [''], 'score_shouquanweize1': [''], 'score_shouquanweize2': [''], 'score_jianlichenggongtuandui1': [''], 'score_jianlichenggongtuandui2': [''], 'score_guanlirenjiguanxi1': [''], 'score_guanlirenjiguanxi2': [''], 'score_yingxiangli1': [''], 'score_yingxiangli2': [''], 'score_jianlihuobanguanxi1': [''], 'score_jianlihuobanguanxi2': [''], 'score_jihuayuzuzhi1': [''], 'score_jihuayuzuzhi2': [''], 'submit': [' Inquire about ']}>

View views.py Amend to read :

  • views.py
#  Establish the result interface of data reverse query 
@login_required
def data_search_results(request):
    #  Query out user maximal query_set
    user_permission_query = user_permission(request)
    
    #  Receive query conditions 
    if 'submit' in request.GET and request.GET['submit']:
        # request.GET.get() avoid KeyError
        #print('request.GET2',request.GET)
        score_jieshoufankui1 = request.GET.get('score_jieshoufankui1', '')
        score_jieshoufankui2 = request.GET.get('score_jieshoufankui2', '')
        score_xueximinruidu1 = request.GET.get('score_xueximinruidu1', '')
        score_xueximinruidu2 = request.GET.get('score_xueximinruidu2', '')
        score_jieguodaoxiang1 = request.GET.get('score_jieguodaoxiang1', '')
        score_jieguodaoxiang2 = request.GET.get('score_jieguodaoxiang2', '')
        score_quanjusiwei1 = request.GET.get('score_quanjusiwei1', '')
        score_quanjusiwei2 = request.GET.get('score_quanjusiwei2', '')
        score_shiyingli1 = request.GET.get('score_shiyingli1', '')
        score_shiyingli2 = request.GET.get('score_shiyingli2', '')
        score_chengjiutaren1 = request.GET.get('score_chengjiutaren1', '')
        score_chengjiutaren2 = request.GET.get('score_chengjiutaren2', '')
        score_lingdaoyiyuan1 = request.GET.get('score_lingdaoyiyuan1', '')
        score_lingdaoyiyuan2 = request.GET.get('score_lingdaoyiyuan2', '')
        score_pinghengrenjiyurenwu1 = request.GET.get('score_pinghengrenjiyurenwu1', '')
        score_pinghengrenjiyurenwu2 = request.GET.get('score_pinghengrenjiyurenwu2', '')
        score_fudao1 = request.GET.get('score_fudao1', '')
        score_fudao2 = request.GET.get('score_fudao2', '')
        score_shouquanweize1 = request.GET.get('score_shouquanweize1', '')
        score_shouquanweize2 = request.GET.get('score_shouquanweize2', '')
        score_jianlichenggongtuandui1 = request.GET.get('score_jianlichenggongtuandui1', '')
        score_jianlichenggongtuandui2 = request.GET.get('score_jianlichenggongtuandui2', '')
        score_guanlirenjiguanxi1 = request.GET.get('score_guanlirenjiguanxi1', '')
        score_guanlirenjiguanxi2 = request.GET.get('score_guanlirenjiguanxi2', '')
        score_yingxiangli1 = request.GET.get('score_yingxiangli1', '')
        score_yingxiangli2 = request.GET.get('score_yingxiangli2', '')
        score_jianlihuobanguanxi1 = request.GET.get('score_jianlihuobanguanxi1', '')
        score_jianlihuobanguanxi2 = request.GET.get('score_jianlihuobanguanxi2', '')
        score_jihuayuzuzhi1 = request.GET.get('score_jihuayuzuzhi1', '')
        score_jihuayuzuzhi2 = request.GET.get('score_jihuayuzuzhi2', '')

        #  Increase the default value 
        if score_jieshoufankui1=='':score_jieshoufankui1=0
        if score_jieshoufankui2=='':score_jieshoufankui2=100
        if score_xueximinruidu1=='':score_xueximinruidu1=0
        if score_xueximinruidu2=='':score_xueximinruidu2=100
        if score_jieguodaoxiang1=='':score_jieguodaoxiang1=0
        if score_jieguodaoxiang2=='':score_jieguodaoxiang2=100
        if score_quanjusiwei1=='':score_quanjusiwei1=0
        if score_quanjusiwei2=='':score_quanjusiwei2=100
        if score_shiyingli1=='':score_shiyingli1=0
        if score_shiyingli2=='':score_shiyingli2=100
        if score_chengjiutaren1=='':score_chengjiutaren1=0
        if score_chengjiutaren2=='':score_chengjiutaren2=100
        if score_lingdaoyiyuan1=='':score_lingdaoyiyuan1=0
        if score_lingdaoyiyuan2=='':score_lingdaoyiyuan2=100
        if score_pinghengrenjiyurenwu1=='':score_pinghengrenjiyurenwu1=0
        if score_pinghengrenjiyurenwu2=='':score_pinghengrenjiyurenwu2=100
        if score_fudao1=='':score_fudao1=0
        if score_fudao2=='':score_fudao2=100
        if score_shouquanweize1=='':score_shouquanweize1=0
        if score_shouquanweize2=='':score_shouquanweize2=100
        if score_jianlichenggongtuandui1=='':score_jianlichenggongtuandui1=0
        if score_jianlichenggongtuandui2=='':score_jianlichenggongtuandui2=100
        if score_guanlirenjiguanxi1=='':score_guanlirenjiguanxi1=0
        if score_guanlirenjiguanxi2=='':score_guanlirenjiguanxi2=100
        if score_yingxiangli1=='':score_yingxiangli1=0
        if score_yingxiangli2=='':score_yingxiangli2=100
        if score_jianlihuobanguanxi1=='':score_jianlihuobanguanxi1=0
        if score_jianlihuobanguanxi2=='':score_jianlihuobanguanxi2=100
        if score_jihuayuzuzhi1=='':score_jihuayuzuzhi1=0
        if score_jihuayuzuzhi2=='':score_jihuayuzuzhi2=100



        #  Create a string for query criteria 
        str_list = Q(score_jieshoufankui__range=(score_jieshoufankui1,score_jieshoufankui2)) 
        str_list = str_list & Q(score_xueximinruidu__range=(score_xueximinruidu1,score_xueximinruidu2)) 
        str_list = str_list & Q(score_jieguodaoxiang__range=(score_jieguodaoxiang1,score_jieguodaoxiang2)) 
        str_list = str_list & Q(score_quanjusiwei__range=(score_quanjusiwei1,score_quanjusiwei2)) 
        str_list = str_list & Q(score_shiyingli__range=(score_shiyingli1,score_shiyingli2)) 
        str_list = str_list & Q(score_chengjiutaren__range=(score_chengjiutaren1,score_chengjiutaren2)) 
        str_list = str_list & Q(score_lingdaoyiyuan__range=(score_lingdaoyiyuan1,score_lingdaoyiyuan2)) 
        str_list = str_list & Q(score_pinghengrenjiyurenwu__range=(score_pinghengrenjiyurenwu1,score_pinghengrenjiyurenwu2)) 
        str_list = str_list & Q(score_fudao__range=(score_fudao1,score_fudao2))
        str_list = str_list & Q(score_shouquanweize__range=(score_shouquanweize1,score_shouquanweize2)) 
        str_list = str_list & Q(score_jianlichenggongtuandui__range=(score_jianlichenggongtuandui1,score_jianlichenggongtuandui2)) 
        str_list = str_list & Q(score_guanlirenjiguanxi__range=(score_guanlirenjiguanxi1,score_guanlirenjiguanxi2)) 
        str_list = str_list & Q(score_yingxiangli__range=(score_yingxiangli1,score_yingxiangli2)) 
        str_list = str_list & Q(score_jianlihuobanguanxi__range=(score_jianlihuobanguanxi1,score_jianlihuobanguanxi2)) 
        str_list = str_list & Q(score_jihuayuzuzhi__range=(score_jihuayuzuzhi1,score_jihuayuzuzhi2)) 


        employee_list = Employee.objects.filter(str_list)  & user_permission_query
        print(str_list)
        return render(request, 'PPDASH/data_search_results.html',context={
    'employee_list': employee_list})
    else:
        return render(request, 'PPDASH/data_search_form.html',context={
    })


  • data_search_form.html
{
    % extends 'PPDASH/base.html' %}
{
    % load static %}

{
    % block title_block %}
    search_form
{
    % endblock %}

{
    % block body_block %}
<div class="container">
    <div class="row py-4 align-items-center">

        <form action="/PPDASH/data_search_results/" method="get">
            <label class="row">
                 Please enter criteria to query &nbsp;&nbsp;&nbsp;&nbsp;
                 Low value &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                 High value 
            </label>
            <div class="row">
                <div class="col-6">
                    <div class="form-group row">
                        <label class="col-3"> Accept feedback </label>
                        <input type="number" name="score_jieshoufankui1" class="col-2">
                        <input type="number" name="score_jieshoufankui2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Learning acumen </label>
                        <input type="number" name="score_xueximinruidu1" class="col-2">
                        <input type="number" name="score_xueximinruidu2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Result oriented </label>
                        <input type="number" name="score_jieguodaoxiang1" class="col-2">
                        <input type="number" name="score_jieguodaoxiang2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Think globally </label>
                        <input type="number" name="score_quanjusiwei1" class="col-2">
                        <input type="number" name="score_quanjusiwei2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Adaptability </label>
                        <input type="number" name="score_shiyingli1" class="col-2">
                        <input type="number" name="score_shiyingli2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Achieve others </label>
                        <input type="number" name="score_chengjiutaren1" class="col-2">
                        <input type="number" name="score_chengjiutaren2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Leadership will </label>
                        <input type="number" name="score_lingdaoyiyuan1" class="col-2">
                        <input type="number" name="score_lingdaoyiyuan2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Balance people and tasks </label>
                        <input type="number" name="score_pinghengrenjiyurenwu1" class="col-2">
                        <input type="number" name="score_pinghengrenjiyurenwu2" class="col-2 mx-1">
                    </div>
                </div>
                <div class="col-6">
                    <div class="form-group row">
                        <label class="col-3"> Coach </label>
                        <input type="number" name="score_fudao1" class="col-2">
                        <input type="number" name="score_fudao2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Delegation of authority </label>
                        <input type="number" name="score_shouquanweize1" class="col-2">
                        <input type="number" name="score_shouquanweize2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Build a successful team </label>
                        <input type="number" name="score_jianlichenggongtuandui1" class="col-2">
                        <input type="number" name="score_jianlichenggongtuandui2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Managing relationships </label>
                        <input type="number" name="score_guanlirenjiguanxi1" class="col-2">
                        <input type="number" name="score_guanlirenjiguanxi2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> influence </label>
                        <input type="number" name="score_yingxiangli1" class="col-2">
                        <input type="number" name="score_yingxiangli2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Building partnerships </label>
                        <input type="number" name="score_jianlihuobanguanxi1" class="col-2">
                        <input type="number" name="score_jianlihuobanguanxi2" class="col-2 mx-1">
                    </div>
                    <div class="form-group row">
                        <label class="col-3"> Planning and organization </label>
                        <input type="number" name="score_jihuayuzuzhi1" class="col-2">
                        <input type="number" name="score_jihuayuzuzhi2" class="col-2 mx-1">
                    </div>
                </div>
            </div>
            <input type="submit" name="submit" value=" Inquire about ">
        </form>

    </div>
</div>
{
    % endblock %}
  • data_search_results.html
{
    % extends 'PPDASH/base.html' %}
{
    % load static %}

{
    % block title_block %}
    search_results
{
    % endblock %}

{
    % block body_block %}


<!--  adjustment color Of js Script start  -->
<script>
    window.onload = function(){
    
        paradom1 = document.getElementsByClassName("p1")
        console.log(paradom1)
        for (i = 0 ; i < paradom1.length; i++) {
    
            var para = document.getElementsByClassName("p1")[i];if (para.textContent <=35) {
    para.style.color = "red";}else if (para.textContent <65) {
    para.style.color = "orange";}else{
    para.style.color = "green";}
        }
    }
</script>
<!--  adjustment color Of js Script Introduction  -->



<div class="container-fluid">
    <p class="my-3">
         The query results are as follows &nbsp;&nbsp;
        <a href="/PPDASH/compare_by_different_employee/{
    { employee_list }}/" > Personal comparison chart ( front 20 name )</a>
    </p>
    <table class="table table-striped table-bordered">
        <thead>
            <tr style="color:White;background-color:#3366FF;font-family: Microsoft is in bold ,Tahoma,Arial, Microsoft elegant bold ;font-size:10px;">
                <th scope="col"> Job number </th>
                <th scope="col" style="width:80px;"> full name </th>
                <th scope="col"> Department </th>
                <th scope="col">Level</th>
                <th scope="col">Function</th>
                <th scope="col"> batch </th>
                <th scope="col"> Accept feedback </th>
                <th scope="col"> Learning acumen </th>
                <th scope="col"> Result oriented </th>
                <th scope="col"> Think globally </th>
                <th scope="col"> Adaptability </th>
                <th scope="col"> Achieve others </th>
                <th scope="col"> Leadership will </th>
                <th scope="col"> Balance people and tasks </th>
                <th scope="col"> Coach *</th>
                <th scope="col"> Delegation of authority *</th>
                <th scope="col"> Build a successful team *</th>
                <th scope="col"> Managing relationships *</th>
                <th scope="col"> influence *</th>
                <th scope="col"> Building partnerships *</th>
                <th scope="col"> Planning and organization *</th>
            </tr>
        </thead>

    
        {
    % for employee in employee_list %}
        <tr valign="middle" style="color:Black;border-color:#E0E0E0;font-size:10px;">
            <td><a href="/PPDASH/search_results/{
    { employee.eid }}/">{
    {
     employee.eid }}</a></td>
            <td>{
    {
     employee.name }}</td>
            <td>{
    {
     employee.dept_code }}</td>
            <td>{
    {
     employee.level }}</td>
            <td>{
    {
     employee.function }}</td>
            <td>{
    {
     employee.batch }}</td>
            <td class="p1">{
    {
     employee.score_jieshoufankui }}</td>
            <td class="p1">{
    {
     employee.score_xueximinruidu }}</td>
            <td class="p1">{
    {
     employee.score_jieguodaoxiang }}</td>
            <td class="p1">{
    {
     employee.score_quanjusiwei }}</td>
            <td class="p1">{
    {
     employee.score_shiyingli }}</td>
            <td class="p1">{
    {
     employee.score_chengjiutaren }}</td>
            <td class="p1">{
    {
     employee.score_lingdaoyiyuan }}</td>
            <td class="p1">{
    {
     employee.score_pinghengrenjiyurenwu }}</td>
            <td class="p1">{
    {
     employee.score_fudao }}</td>
            <td class="p1">{
    {
     employee.score_shouquanweize }}</td>
            <td class="p1">{
    {
     employee.score_jianlichenggongtuandui }}</td>
            <td class="p1">{
    {
     employee.score_guanlirenjiguanxi }}</td>
            <td class="p1">{
    {
     employee.score_yingxiangli }}</td>
            <td class="p1">{
    {
     employee.score_jianlihuobanguanxi }}</td>
            <td class="p1">{
    {
     employee.score_jihuayuzuzhi }}</td>
        </tr>
        {
    % endfor %}

    </table>
</div>

<!-- The information is left blank for a short time -->
{
    % if not employee_list|length >= 3 %}
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
{
    % endif %}

{
    % endblock %}

版权声明
本文为[i see the future]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/11/20211125170847169I.html

Scroll to Top