最大递归深度超出请求

时间:2018-04-09 11:40:02

标签: python django python-3.x wagtail

目前对python / django来说是新手,请原谅我,如果这是一个简单的问题。

我正在使用请求下载json文件,并尝试解析/显示模板中的值。

我收到以下错误: `调用Python对象时超出了最大递归深度

代码如下:

from django import template
from django.template.loader import render_to_string
from wagtail.wagtailcore.blocks.base import Block
from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader

import datetime
import urllib
import json
import requests
import requests_cache

requests_cache.install_cache('demo_cache')

register = template.Library()

# Advert snippets
@register.inclusion_tag('swd/templates/adverts.html', takes_context=True)
def adverts(context):
    return {
        'adverts': Advert.objects.all(),
        'request': context['request'],
    }

#Socialboard
#Pinterest
@register.inclusion_tag('blocks/socialboard.html')
def get_pinterest_pins():
    req = requests.get(URL)
    req_json = json.loads(req.content)

    for key, value in req_json.items():
        print (key, value)

# Related within article
@register.filter
def inject_related_after_paragraph(value, arg):
    related_list = render_to_string("related.list.html")
    paragraphs = value.split('</p>')

    if arg < len(paragraphs):
        paragraphs[arg] = related_list + paragraphs[arg]
        value = '</p>'.join(paragraphs)
    return value

@register.filter
def inject_advert_after_paragraph(value, arg):
    advert = render_to_string("adverts.html")
    paragraphs = value.split('</p>')

    if arg < len(paragraphs):
        paragraphs[arg] = advert + paragraphs[arg]
        value = '</p>'.join(paragraphs)

    return value

这会循环播放网址,但会一直持续到超时。

1 个答案:

答案 0 :(得分:1)

以下代码完美有效:

import requests
import json

req = requests.get('https://jsonplaceholder.typicode.com/posts/1')
req_json = json.loads(req.content)

for key, value in req_json.items():
    print (key, value)

根据URL,生成的JSON可能“嵌套太深”。 如果json.loads上发生错误,您将不得不找到一个使用堆栈而不是递归调用的JSON解码器。如果print语句上发生错误,请避免打印该字典(或定义您自己的使用堆栈的函数)。