首页 > 解决方案 > TypeError:在 Django 项目中声明上下文处理器时,“NoneType”对象不可迭代

问题描述

我想在不渲染页面的情况下传递上下文值,这就是我实现上下文处理器的原因。但是当我从该函数返回一个上下文值时,它向我显示了这个错误。这是我的payment.py代码:

def payment_url(request):
    todays_date = date.today()
    now = datetime.datetime.now()
    mcnt_TxnNo = "Txn" + str(todays_date.year) + str('{:02d}'.format(
    todays_date.month)) + str(todays_date.day) + str(now.hour) + 
    str(now.minute) + 
    str('{:02d}'.format(now.second))
    order_no = "ON" + str(todays_date.year) + str('{:02d}'.format(
    todays_date.month)) + str(todays_date.day) + str(now.hour) + 
    str(now.minute) + 
    str('{:02d}'.format(now.second))
    secret_key = b"***"
    print(mcnt_TxnNo)
    if request.method == 'POST':
        name = request.POST["clientName"]
        phone = request.POST["phone"]
        address = request.POST["address"]
        email = request.POST["clientEmail"]
        product_name = request.POST["proName"]
        print("product Name", product_name)
        paid = request.POST["paid"]
        print(name)

        string = "***"
        print(string)
        byte_string = bytes(string, 'utf-8')
        hash_code = hmac.new(secret_key.upper(), byte_string, hashlib.sha256)
        hash_text = hash_code.hexdigest()
        print(hash_text)

        jsonObj = {
        // json object
        }

        json_string = json.dumps(jsonObj)
        print(json_string)
        url = "**"
        headers = {'Content-type': 'application/json', 'Accept': 
        'text/plain'}
        r = requests.post(url, data=json_string, headers=headers)
        print(r.json())
        j = r.json()['status']
        k = r.json()['data']

        if j == "200":
            payment_id = k['payment_id']
            redirect_url = k['redirect_url']
            payment_link = str(redirect_url) + "?" + str(payment_id)
            print(payment_link)
            return {'payment_link': payment_link}
        else:
            return {}

还有我的setting.py:

TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [os.path.join(BASE_DIR, 'templates')],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'users.payment.payment_url',
            ],
        },
    },
]

所以,基本上,我正在开发一个项目,在该项目中我创建了一个 JSON 请求并生成了一个 payment_url,所以我返回了它,以便我可以从任何页面访问它。当 payment_url 为 none 时,我还返回了一个空字典,但仍然出现相同的错误。

追溯:

File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\user\Desktop\Django Web App\Len-den\len_den\pages\views.py", line 25, in index
    return render(request, 'pages/index.html')
  File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\shortcuts.py", line 36, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\template\base.py", line 169, in render
    with context.bind_template(self):
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\Users\user\Desktop\Django Web App\Len-den\venv\lib\site-packages\django\template\context.py", line 246, in bind_template
    updates.update(processor(self.request))
TypeError: 'NoneType' object is not iterable

标签: pythondjango

解决方案


当方法为 GET 时返回空字典 {}。


推荐阅读