如何修复“AttributeError at /orders/create/‘tuple’对象在 Django 中没有属性‘get’
Posted
技术标签:
【中文标题】如何修复“AttributeError at /orders/create/‘tuple’对象在 Django 中没有属性‘get’【英文标题】:How to fix "AttributeError at /orders/create/ 'tuple' object has no attribute 'get' in Django 【发布时间】:2019-10-04 23:41:45 【问题描述】:我正在尝试制作一个电子商务网站,用户在其中提交订单并被重定向到支付界面(Paytm,印度)。
但是我得到了这个错误:
Traceback:
File "/Users/shyambalakrishnan/Dev/trydjango/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Users/shyambalakrishnan/Dev/trydjango/lib/python3.7/site-packages/django/utils/deprecation.py" in __call__
96. response = self.process_response(request, response)
File "/Users/shyambalakrishnan/Dev/trydjango/lib/python3.7/site-packages/django/middleware/clickjacking.py" in process_response
26. if response.get('X-Frame-Options') is not None:
Exception Type: AttributeError at /orders/create/
Exception Value: 'tuple' object has no attribute 'get'
如果我将“else 子句”缩进到 if form.is_valid() 的正下方 我收到此错误。
Traceback:
File "/Users/shyambalakrishnan/Dev/trydjango/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Users/shyambalakrishnan/Dev/trydjango/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
126. "returned None instead." % (callback.__module__, view_name)
Exception Type: ValueError at /orders/create/
Exception Value: The view orders.views.order_create didn't return an HttpResponse object. It returned None instead.
虽然在这两种情况下,服务器都运行没有任何错误。
views.py(在我的订单应用中)
from django.shortcuts import render
from .models import OrderItem , Order
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from .forms import OrderCreateForm
from cart.cart import Cart
from payTm import Checksum
from django.http import HttpResponse
MERCHANT_KEY = 'my_merchant_key';
@csrf_exempt
def handlerequest(request):
return HttpResponse('done')
pass
@login_required
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save()
for item in cart:
OrderItem.objects.create(
order=order,
product=item['product'],
price=item['price'],
quantity=item['quantity'],
total_price=item['total_price'],
)
cart.clear()
param_dict =
'MID': 'osIFyR31215257913677',
'ORDER_ID': str(order.id),
'TXN_AMOUNT': str(order.total_cost),
'CUST_ID': order.email,
'INDUSTRY_TYPE_ID': 'Retail',
'WEBSITE': 'WEBSTAGING',
'CHANNEL_ID': 'WEB',
'CALLBACK_URL': 'https://127.0.0.1:8000/handlerequest/'
param_dict['CHECKSUMHASH'] =
Checksum.generate_checksum_by_str(param_dict, MERCHANT_KEY)
return (request, 'paytm.html', 'order': order,
'param_dict': param_dict)
else:
form = OrderCreateForm()
return render(request, 'create.html', 'form': form)
paytm.html
% extends 'base.html' %
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Paytm merchant Payment Page</title>
</head>
<body>
<h1> Redirecting you to the merchant.....</h1>
<h1> Please don't refresh this page.....</h1>
<form action="https://securegw-stage.paytm.in/theia/processTransaction" method="POST" name="paytm">
% for key,value in param_dict.items %
<input type="hidden" name=" key " value=" value ">
% endfor %
</form>
</body>
<script>
document.paytm.submit();
</script>
</html>
我希望将输出发布到 handlerequest 视图,它应该显示完成。 但不幸的是 这没有发生。 同样在 paytm.html..form action= 被发布到 paytm 的测试页面,该页面在 https://developer.paytm.com/docs/v1/payment-gateway
中给出【问题讨论】:
首先,修正缩进。其次,停止发布错误的屏幕截图。在该错误页面上有一个名为“切换到复制和粘贴视图”的链接;单击它并在此处发布结果文本。 哦,对不起。我做了更改。 你不能return (request, 'paytm.html', 'order': order, ...)
,你应该返回一个HttpResponse,该行的末尾还有一个尾逗号。
【参考方案1】:
你忘了打电话给render
。顺便说一句,它需要一个上下文字典,而不是两个。
return render(request, 'paytm.html', 'order': order, 'param_dict': param_dict)
【讨论】:
您好 Daniel 先生,这确实解决了我的问题。谢谢。以上是关于如何修复“AttributeError at /orders/create/‘tuple’对象在 Django 中没有属性‘get’的主要内容,如果未能解决你的问题,请参考以下文章