[英]Passing parameter from template to view in Django
這里是Django的菜鳥。 我對在django中導出csv的問題感到困惑。 我目前正在使用我的模型產品的views.py中的此函數成功導出包含數據庫中所有對象(產品)的csv文件。
def export_csv(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="product-inventory.csv"'
writer = csv.writer(response)
writer.writerow(['Product Name', 'STATUS'])
products = Product.objects.all().values_list('name', 'status')
for product in products:
writer.writerow(product)
return response
這是在我的模板中
<a class="export-btn" id="test" href="{% url 'export_csv' %}">Export</a>
這是url.py
url(r'^export/csv$', product_views.export_csv, name='export_csv')
問題是如何動態更改export_csv
函數中的Product.objects.all()
,使其等於使用搜索(在另一個函數中使用request.GET)從用戶那里獲得的產品列表?
提前致謝。
嘗試添加模板形式:
<form method="GET" action="">
<input type="text" name="q" placeholder="Search">
<input type="submit" value="Search">
</form>
現在,您可以獲取搜索值並過濾產品:
search = request.GET.get('q')
products = Product.objects.filter(name__icontains=search).values_list('name', 'status')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.