여러 제품을 반환 할 수있는이 코드가 있습니다. 이제 마지막 하나만 반환합니다. 변수를 덮어 쓰고 있기 때문이라고 생각하지만 다른 방법을 모르겠습니다.
def result(request):
rawquery = request.GET.get('q') #gets the product name to search from a form
Product_set = Product.objects.filter(name__icontains=rawquery).distinct()
for product in Product_set:
name = product.name
id = product.id
store_name = product.store_name
Price_set = Scan.objects.filter(product=id)
for price in Price_set:
current_price = price.price
context = {
'name': name,
'store_name': store_name,
'price': current_price,
'query': rawquery
}
return render(request, 'result.html', context)
이것은 템플릿입니다
% {extends 'base.html' %}
{% block content %}
<h1>Results for {{ query }} </h1>
<p> {% if name %} {{name}} {% else %} None {% endif %} | {% if store_name %} {{store_name}} {% endif %} | {% if price %} {{price}} {% endif %} </p>
{% endblock content %}
목록에 제품을 추가해야합니다.
def result(request):
rawquery = request.GET.get('q') #gets the product name to search from a form
Product_set = Product.objects.filter(name__icontains=rawquery).distinct()
products = []
for product in Product_set:
price = Scan.objects.filter(product=product.id).first()
products.append({
'name': product.name,
'store_name': product.store_name,
'price': price.price
})
return render(request, 'result.html', {'query': rawquery, 'products': products})
이제 여기에서 제품을 반복 할 수 있습니다.
{% extends 'base.html' %}
{% block content %}
<h1>Results for {{ query }} </h1>
{% for product in products %}
<p> {{product.name}} | {{product.store_name}} | {{product.price}} </p>
{% endfor %}
{% endblock %}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다