我有一个页面,它呈现我的数据库中的对象列表。我需要按下一个按钮,在我的数据库中查询特定对象,在我的视图中执行一些操作,并返回我希望在弹出气泡中显示在同一页面上而不重新加载它的价格值,代替我有填充文本。我已经尝试通过创建一个带有id
在值字段中包含对象的按钮的表单,并将该值发送到我的原始页面视图,然后我调用一个函数来处理我想用那个 id 做的事情。我请求对象id
,将其传递给新的视图函数,然后查询我的数据库并尝试返回值。我做一个if request.POST
在原始页面视图 t 中声明,并将呈现的变量更改为我想要显示的新值。当我按下按钮时,我在弹出气泡中得到相同的填充文本,在我的控制台中我得到以下错误:
ValueError: The view engine.views.search_list didn't return an HttpResponse object. It returned None instead.
因此,每当我调用视图函数时,我似乎都必须返回一个 HTTP 请求。我已经尝试在我的新视图函数中返回一个呈现的响应,并且在我检查if request.POST
并将新值作为上下文传递之后:
return render(request, 'original_page.html', {'value':new_value})
我犯了同样的错误。如何在同一页面上返回我想要的值而不会出现此错误?我尝试使用HttpResponse
空白值,redirect('some_place.html')
但没有成功。我确实有一个 Iframe 可以阻止我的 html 页面重新加载。这是我正在使用的代码:
HTML
<iframe name="submit-frame" style="display: none"></iframe>
<form action="{% url 'search_list' %}" method="post" target="submit-frame">{% csrf_token %}
<button name="productId" value="{{product.id}}" data-toggle="popover" data-animation="true" data-content="{{price}}" type="" id='button'>Reveal Price</button>
</form>
<script type="text/javascript">
$('[data-toggle="popover"]').popover({
container: 'body',
delay: { "show": 100, "fade": 100 },
})
</script>
视图 - 获得新价值的功能
def get_price(request):
product_id = request.POST.get('productId')
item = get_object_or_404(Product, id=product_id)
price = item.price
return price
视图 - 原始渲染视图
def search_list(request):
results = Product.objects.all().order_by('name')
price = 'no_price'
if request.POST:
print(request.POST.get('productId'))
tcg_price = get_price(request)
return render(request, 'search_result_list.html', {'tcg_price': tcg_price})
else: ...
return render(request, 'search_result_list.html', {'price': price, 'results':results})