我有一個 django 模板如下
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Master Title</th>
<th>Retailer title</th>
<th>To add</th>
</tr>
</thead>
<tbody>
{% if d %}
{% for i in d %}
<tr>
<th scope="row">{{forloop.counter}}</th>
<td>{{i.col1}}</td>
<td>{{i.col2}}</td>
<td>
<input type="hidden" name='row_value' value="{{i.col1}}|{{i.col2}}">
<a class='btn btn-success' href="{% url 'match' %}">Match</a>
<a class='btn btn-outline-danger' href="{% url 'mismatch' %}">Not a Match</a>
</td>
</tr>
{% endfor %}
{% endif %}
</tbody>
</table>
單擊匹配按鈕時,我想收回視圖中隱藏輸入的值。這是我的 views.py
def match(request):
print(request.GET.get('row_value'))
print('match')
但這會回傳 None。
我希望 col1 和 col2 的值列印如下
col1|col2
我不確定我哪里出錯了。
uj5u.com熱心網友回復:
你應該在 GET 中使用 Django URL 引數:
網址.py
urlpatterns = [
path('your-path/<int:first_param>/<int:second_param>', views.match),
]
來源:https : //docs.djangoproject.com/en/3.2/topics/http/urls/#example
視圖.py
def match(request, first_param=None, second_param=None):
# then do whatever you want with your params
...
來源:https : //docs.djangoproject.com/en/3.2/topics/http/urls/#specifying-defaults-for-view-arguments
模板:
<td>
<a class='btn btn-outline' href="your-path/{{i.col1}}/{{i.col2}}">Example</a>
</td>
這是一個基本示例,因此請將其更改為您的用例。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/338195.html
標籤:Python 姜戈 django-views django-模板
