html - 超出 Django 最大递归深度。查看无限循环

标签 html python-3.x django django-views

我正在编写一个应用程序,用于解码汽车提供的 VIN 号码的品牌、型号和年份信息。该脚本独立工作,但当集成到我的 django 应用程序中并通过单击我的 Web 应用程序上的按钮调用时, View 开始无限循环并引发 RecursionError。我将在下面提供相关代码

基本 html 模板

<!DOCTYPE html>
{%load static%}

<Html>
  <title>What the F*cking F*ck</title>
  <head>
  
  </head>

  
  <h1>Here I will attempt to recreate the Working Title File in a somewhat nice looking format. </h1>
  <h4>We'll see what happens</h4>
  
  <div>
    {% block header %}
    <p>Upload an IPDF with the Browse Button below</p>
    <form method="post" enctype="multipart/form-data">
      {% csrf_token %}
      {{ form.as_p }}
      <button type="submit">Upload</button>
    </form>
    {% endblock %}
    <p><a href="{% url 'wtf' %}">Return to upload</a></p>
    <p>

  </div>

  <div id="address">
    {% block address %}
      <p>This is where stuff would render if you had uploaded something. But you didn't. Or it broke. So shame on both of us.</p>
    {% endblock %}
  </div>
</Html>

address_render.html(继承自base,也称为wtf)。这按预期工作。当我单击此处的按钮来调用 View vin_decode 时,它会中断。

{% extends "wtf2/wtf.html" %}
<script scr='../../jquery-3.6.0.js'>
</script>
{% block header %}
    <p>Successful Address Validation</p>
    <form action="{% url 'vin' %}" method='post'>
    {% csrf_token %}
    <button type="submit">Click here to begin VIN Decode</button>
    </form>
{% endblock %}

{% block address %}
<div>
    <p>Sick! The address validation portion worked. Now what? Please Check the returned address. This one came back invalid</p>
    <table>
        <thead>
            <tr>
                <th>Street Lines</th>
                <th>City</th>
                <th>State</th>
                <th>ZIP</th>
                <th>Returned StreetLines</th>
                <th>Returned City</th>
                <th>Returned State</th>
                <th>Returned ZIP</th>
                <th>Returned Status</th>
                <th>Resolved</th>
                <th>Delivery Point Valid</th>
                <th>Interpolated Street Address</th>
                <th>City Match?</th>
                <th>ZIP Match?</th>
                <th>Resolved Address?</th>
                <th>Valid?</th>
            </tr>
        </thead>
        <tbody>
        {% if d %}
        {% for i in d%}
        
            <tr>
            {% if i.Valid == "No"%}
                <td>{{ i.StreetLines }}</td>
                <td>{{ i.City }}</td>
                <td>{{ i.State }}</td>
                <td>{{ i.ZIP }}</td>
                <td>{{ i.Returned_StreetLines }}</td>
                <td>{{ i.Returned_City }}</td>
                <td>{{ i.Returned_State }}</td>
                <td>{{ i.Returned_ZIP }}</td>
                <td>{{ i.Returned_Status }}</td>
                <td>{{ i.Resolved }}</td>
                <td>{{ i.DPV }}</td>
                <td>{{ i.InterpolatedStreetAddress }}</td>
                <td>{{ i.City_Match }}</td>
                <td>{{ i.ZIP_Match }}</td>
                <td>{{ i.Resolved_Address }}</td>
                <td>{{ i.Valid }}</td>
            {% endif %}
            </tr>
        {% endfor %}
        {% endif %}
        </tbody>
    </table>
</div>
{% endblock %}

vin_decode.html(也继承自base.html)

{% extends "wtf2/wtf.html" %}
{% comment %} <script scr='../../jquery-3.6.0.js'>
</script> {% endcomment %}
{% block header %}
    <p>Successful VIN Decoding</p>
    <p><a href="{% url 'wtf' %}">Click here to return to upload</a></p>

    
{% endblock %}

{% block address %}
<div>
    <p>Sick! The VIN Decoding worked!</p>
    <table>
        <thead>
            <tr>
                <th>Site</th>
                <th>Vehicle Number</th>
                <th>VIN</th>
                <th>Current Device</th>
                <th>Make</th>
                <th>Model</th>
                <th>Year</th>
                <th>Old Make</th>
                <th>Old Model</th>
                <th>Old Year</th>
            </tr>
        </thead>
        <tbody>
        {% if d %}
        {% for i in d%}
        
            <tr>
                <td>{{ i.site }}</td>
                <td>{{ i.vehicle_number }}</td>
                <td>{{ i.vin }}</td>
                <td>{{ i.current_device }}</td>
                <td>{{ i.Returned_Make }}</td>
                <td>{{ i.Returned_Model }}</td>
                <td>{{ i.Returned_Year }}</td>
                <td>{{ i.make }}</td>
                <td>{{ i.model }}</td>
                <td>{{ i.year }}</td>
            </tr>
        {% endfor %}
        {% endif %}
        </tbody>
    </table>
</div>
{% endblock %}

View .py

from django.http.response import HttpResponseRedirect
from django.shortcuts import render
from suds.plugin import Context
from .models import Document
from .forms import DocumentForm
import pandas as pd
import sqlite3
import json
from .Scripts.address_validation import address_validation
from .Scripts.vin_decode import vin_decode
from .Scripts.data_collect import *

# cnxn_string = sqlite3.connect('db.sqlite3')

# Create your views here.
def doc_upload(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            doc = Document.objects.latest('id')
            
            # project_upload(doc.document.path, doc.pk)
            # vehicle_upload(doc.document.path, doc.pk)
            data_collection(doc.document.path,doc.pk)
            df = address_validation(doc.pk)
            json_records = df.reset_index().to_json(orient='records')
            data = json.loads(json_records)
            context = {'d': data}
            return render(request, 'wtf2/address_render.html', context)
    else:
        form = DocumentForm()
    return render(request, 'wtf2/wtf.html', {
        'form': form
    })

def vin_decode(request):
    print('Is this thing on')
    doc = Document.objects.latest('id')
    df = vin_decode(doc.pk)
    json_records = df.reset_index().to_json(orient='records')
    df = json.loads(json_records)
    context = {'d': df}
    return render(request, 'wtf2/vin_decode.html', context)

url.py

from django.contrib import admin
from django.urls import path
from introduction import views
from wtf2 import views as wtviews
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('team', views.team, name='team'),
    path('team', views.showdetails, name='PPTracker'),
    path('wtf', wtviews.doc_upload, name='wtf'),
    path('wtf/vin_decode', wtviews.vin_decode, name='vin')
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

知道这是怎么回事吗?我收到一个 RecursionError ,它重复调用 View 的第一行“vin_decode”。我添加了一个调试

print('Is this thing on')

在 vin_decode View 中进行测试,终端上的错误代码在中断之前会打印“Is this thing on”1000 次。还将添加完整的错误消息。

Is this thing on
Internal Server Error: /wtf/vin_decode
Traceback (most recent call last):
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\ross.martin\Documents\GitHub\Django-Unchained\website-Project\wtf2\views.py", line 40, in vin_decode
    df = vin_decode(doc.pk)
  File "C:\Users\ross.martin\Documents\GitHub\Django-Unchained\website-Project\wtf2\views.py", line 40, in vin_decode
    df = vin_decode(doc.pk)
  File "C:\Users\ross.martin\Documents\GitHub\Django-Unchained\website-Project\wtf2\views.py", line 40, in vin_decode
    df = vin_decode(doc.pk)
  [Previous line repeated 938 more times]
  File "C:\Users\ross.martin\Documents\GitHub\Django-Unchained\website-Project\wtf2\views.py", line 39, in vin_decode
    doc = Document.objects.latest('id')
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 674, in latest
    return self.reverse()._earliest(*fields)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 668, in _earliest
    return obj.get()
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 425, in get
    num = len(clone)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 269, in __len__
    self._fetch_all()
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1143, in execute_sql
    sql, params = self.as_sql()
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 498, in as_sql
    extra_select, order_by, group_by = self.pre_sql_setup()
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 56, in pre_sql_setup
    order_by = self.get_order_by()
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 361, in get_order_by
    resolved = expr.resolve_expression(self.query, allow_joins=True, reuse=None)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\expressions.py", line 249, in resolve_expression
    c.set_source_expressions([
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\expressions.py", line 250, in <listcomp>
    expr.resolve_expression(query, allow_joins, reuse, summarize)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\expressions.py", line 247, in resolve_expression
    c = self.copy()
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\expressions.py", line 348, in copy
    return copy.copy(self)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\copy.py", line 102, in copy
    return _reconstruct(x, None, *rv)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\copy.py", line 264, in _reconstruct
    y = func(*args)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\copyreg.py", line 95, in __newobj__
    return cls.__new__(cls, *args)
  File "C:\Users\ross.martin\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\deconstruct.py", line 16, in __new__
    obj = super(klass, cls).__new__(cls)
RecursionError: maximum recursion depth exceeded while calling a Python object

最佳答案

您将 vin_decode 名称用于两件事:导入一个函数

from .Scripts.vin_decode import vin_decode

然后是 View 本身。

df = vin_decode(doc.pk)中,您认为您正在调用该函数,但 View 在无限循环中调用自身。

最简单的解决方案:用另一个名称命名您的 View ,例如 vin_decode_view

关于html - 超出 Django 最大递归深度。查看无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67338276/

相关文章:

django - 显示更多突出显示的上下文

javascript - 我如何修改脚本以打印 mysql 查询中的所有元素 - php mysql javascript ajax

javascript - 使用语义 ui 的动态下拉列表

javascript - 使单个 div 的滚动条使用整页滚动条

python - BeautifulSoup 无法工作,出现 NoneType 错误

python - Docker for django 开发自动更新

php - 输入从右到左 html/php

python - 为什么在 python 3.5 中使用 asyncio 时出现被忽略的异常

Django 翻译 RTL 语言

Django - 覆盖保存方法时检查旧值和新值之间的差异