简体   繁体   English

赋值前引用的 UnboundLocalError 局部变量 'context' Django

[英]UnboundLocalError local variable 'context' referenced before assignment Django

I get the error below immediately after i add a new root url inside the root urls.py.在根 urls.py 中添加新根 url 后,我立即收到以下错误。

When i remove the dashboard view, url and i try to load index view, it renders successfully.当我删除仪表板视图 url 并尝试加载索引视图时,它会成功呈现。 What am i doing wrong or what can i do to resolve the issue.我做错了什么或者我能做些什么来解决这个问题。

Error message错误信息

UnboundLocalError at /blogapp/
local variable 'context' referenced before assignment
Request Method: GET
Request URL:    http://127.0.0.1:8000/blogapp/
Django Version: 4.0.2
Exception Type: UnboundLocalError
Exception Value:    
local variable 'context' referenced before assignment

My views我的看法

from multiprocessing import context
import re
from django.shortcuts import render
from django.http import HttpResponse
from .odd_finder_func import *


def index(request):
    if request.method == 'POST':
        odd1 = float(request.POST.get('odd1'))
        odd2 = float(request.POST.get('odd2'))
        odd3 = float(request.POST.get('odd3'))

        func_def = odd_finder_true(odd1, odd2, odd3)

        context = {
            'Juice': func_def['Juice'],
            'TotalImpliedProbability': func_def['TotalImpliedProbability'],
            'HomeOdd': func_def['HomeOdd'],
            'DrawOdd': func_def['DrawOdd'],
            'AwayOdd': func_def['AwayOdd'],
            'Home_True_Odd': func_def['Home_True_Odd'],
            'Draw_True_Odd': func_def['Draw_True_Odd'],
            'Away_True_Odd': func_def['Away_True_Odd'],
            'True_Probability': func_def['True_Probability']
            }

        context = context

    return render(request, 'index.html', context)

def dashboard(request):
    return render(request, 'dashboard.html')

blogapp urls.py博客应用程序 urls.py

from django.urls import path
from .views import *
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

myblog urls.py the root file. myblog urls.py 根文件。

from django.contrib import admin
from django.urls import path, include
from blogapp import views

urlpatterns = [ 
    path('admin/', admin.site.urls),
    path('', views.dashboard, name='dashboard'),
    path('blogapp/', include('blogapp.urls')),
]

index.html index.html

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    <div class="container">
        <!-- Content here -->

        
    <form action="" method="post">
        {% csrf_token %}
        <div class="mb-3">
            <label for="Odd1" class="form-label">Home Odd</label>
            <input type="number" class="form-control" name="odd1" id="odd1" min="0" value=" " step=".01" required='required'>
          </div>
        <div class="mb-3">
          <label for="Odd2" class="form-label">Draw Odd</label>
          <input type="number" class="form-control" name="odd2" id="odd2" min="0" value=" " step=".01" required='required'>
        </div>

        <div class="mb-3">
            <label for="Odd3" class="form-label">Away Odd</label>
            <input type="number" class="form-control" name="odd3" id="odd3" min="0" value=" " step=".01" required='required'>
          </div>
       
        <button type="submit" class="btn btn-primary">Submit</button>
        <input class="btn btn-primary" type="reset" value="Reset">
      </form>

      </div>
       

      <div class="container">
        <p>Total Implied probability percentage: {{TotalImpliedProbability}}</p>
        <p>Bookie juice is: {{Juice}}</p>
        <p>Home Odd: {{HomeOdd}}</p>
        <p>Draw Odd: {{DrawOdd}}</p>
        <p>Away Odd: {{AwayOdd}}</p>
        <p>Home True Odd: {{Home_True_Odd}}</p>
        <p>Draw True Odd: {{Draw_True_Odd}}</p>
        <p>Away True Odd: {{Away_True_Odd}}</p>
        <p>True Probability is: {{True_Probability}}</p>
       
      </div>

      



    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>


    
    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
    -->
  </body>
</html>

Below is an attachment of the django app files is set up.下面是django app文件设置的附件。

在此处输入图像描述

You need to define context when if request.method == "GET"你需要在if request.method == "GET"时定义上下文

def index(request):
    if request.method == "POST":
        odd1 = float(request.POST.get("odd1"))
        odd2 = float(request.POST.get("odd2"))
        odd3 = float(request.POST.get("odd3"))

        func_def = odd_finder_true(odd1, odd2, odd3)

        context = {
            "Juice": func_def["Juice"],
            "TotalImpliedProbability": func_def["TotalImpliedProbability"],
            "HomeOdd": func_def["HomeOdd"],
            "DrawOdd": func_def["DrawOdd"],
            "AwayOdd": func_def["AwayOdd"],
            "Home_True_Odd": func_def["Home_True_Odd"],
            "Draw_True_Odd": func_def["Draw_True_Odd"],
            "Away_True_Odd": func_def["Away_True_Odd"],
            "True_Probability": func_def["True_Probability"],
        }

        context = context

        # INDENT THIS
        return render(request, "index.html", context)
    else:
        # WHAT IS THE CONTEXT WHEN request.method == "GET" ?
        return render(request, "index.html", {})

The problem is that the variable context you created has the same name as the context you imported from multiprocessing at the top of the file.问题是您创建的变量context与您从文件顶部的 multiprocessing 导入的context同名。 Changing the variable name should fix the problem.更改变量名称应该可以解决问题。

from multiprocessing import context
import re
from django.shortcuts import render
from django.http import HttpResponse
from .odd_finder_func import *


def index(request):
    if request.method == 'POST':
        odd1 = float(request.POST.get('odd1'))
        odd2 = float(request.POST.get('odd2'))
        odd3 = float(request.POST.get('odd3'))

        func_def = odd_finder_true(odd1, odd2, odd3)

        response_context = {
            'Juice': func_def['Juice'],
            'TotalImpliedProbability': func_def['TotalImpliedProbability'],
            'HomeOdd': func_def['HomeOdd'],
            'DrawOdd': func_def['DrawOdd'],
            'AwayOdd': func_def['AwayOdd'],
            'Home_True_Odd': func_def['Home_True_Odd'],
            'Draw_True_Odd': func_def['Draw_True_Odd'],
            'Away_True_Odd': func_def['Away_True_Odd'],
            'True_Probability': func_def['True_Probability']
            }

    return render(request, 'index.html', response_context)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分配前引用的 UnboundLocalError 局部变量“上下文” - UnboundLocalError local variable 'context' referenced before assignment UnboundLocalError at / local variable 'context' 在赋值之前引用 - UnboundLocalError at / local variable 'context' referenced before assignment UnboundLocalError:分配前已引用局部变量“ r”(Django) - UnboundLocalError: local variable 'r' referenced before assignment (Django) UnboundLocalError:在Django中赋值之前引用了局部变量“ form” - UnboundLocalError: local variable 'form' referenced before assignment in Django UnboundLocalError-分配前引用的局部变量-Django - UnboundLocalError- local variable referenced before assignment - Django UnboundLocalError:在 DJANGO 中赋值之前引用了局部变量“formset” - UnboundLocalError: local variable 'formset' referenced before assignment in DJANGO Django UnboundLocalError:分配前引用的局部变量“标签” - Django UnboundLocalError: local variable 'tags' referenced before assignment 在 Django 中分配之前引用 /signin/ 局部变量“用户”处的 UnboundLocalError - UnboundLocalError at /signin/ local variable 'user' referenced before assignment in Django Django - UnboundLocalError:分配前引用的局部变量“image_link” - Django - UnboundLocalError: local variable 'image_link' referenced before assignment Django上下文:赋值前引用的局部变量 - Django context : local variable referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM