我當前的頁面有一個過濾器,用戶可以根據需要填寫。
def charts_wallet_view(request):
result = {}
filtered_date1 = request.GET.get('fecha_inicio', '')
filtered_date2 = request.GET.get('fecha_fin', '')
context = {
'mes_inicio' : mes_inicio,
'mes_fin' : mes_fin,
}
return render(request, 'wallet.html', context)
在 wallet.html 中有一個按鈕,可以將用戶重定向到下載檔案的新 url:
<a class="gpg-button egp secondary no_decoration" href="{% url "export_wallet" %}">
我的問題是:如何將引數(fecha_inicio 和 fecha_fin)傳遞給新的 url/view?首先我想到做這樣的事情:
<a class="gpg-button egp secondary no_decoration" href="{% url "export_wallet" mes_inicio mes_fin %}">
但它不起作用。然后我認為我還需要在 url.py 中添加兩個引數:
path('export_wallet/<str:mes_inicio>/<str:fecha_fin >', views.export_wallet_view, name='export_wallet')
在 export_wallet 視圖中:
def charts_wallet_view(request, mes_inicio,mes_fin):
#view logic
但是當引數中沒有傳遞任何值時,這不起作用。
uj5u.com熱心網友回復:
編輯因為問題改變了:在這里做一個 POST 可能比 GET 更有意義。
path('export_wallet/', views.export_wallet_view, name='export_wallet'),
.inline {
display: inline;
}
.link-button {
background: none;
border: none;
color: blue;
text-decoration: underline;
cursor: pointer;
font-size: 1em;
font-family: serif;
}
.link-button:focus {
outline: none;
}
.link-button:active {
color:red;
}
<form method="post" action="{% url 'export_wallet' %}" class="inline">
<input type="hidden" name="fecha_inicio" value="{{ fecha_inicio }}">
<input type="hidden" name="fecha_fin" value="{{ fecha_fin }}">
<button type="submit" name="submit_param" value="submit_value" class="link-button">
This is a link that sends a POST request
</button>
</form>
然后在您的 export_wallet_view 中:
fecha_fin = request.POST.get("fecha_fin")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/469356.html
