【发布时间】:2020-09-30 15:27:50
【问题描述】:
我正在开发一个 django 网络应用程序。该应用程序在表单中有几个按钮。这些按钮的作用类似于拨动开关。当用户提交表单时,我想访问 django views 中按钮的值。请看下图以更好地理解。
我用 Bootstrap 设计了表单。这是 HTML 表单的代码。
<form>
{% csrf_token %}
...
<p class='details-scrape'>Select the items you want to add to the list</p>
<button type="button" class="btn btn-outline-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
Tomato
</button>
<button type="button" class="btn btn-outline-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
Eggs
</button>
<button type="button" class="btn btn-outline-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
Bread
</button>
<button type="button" class="btn btn-outline-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
Beans
</button>
<button type="button" class="btn btn-outline-primary" data-toggle="button" aria-pressed="false" autocomplete="off">
Cheese
</button>
<button type="submit" class="btn btn-primary btn-block btn-lg mt-5">Submit</button>
</form>
这是我的views.py代码
def index(request):
if request.method == 'POST':
# ------------- New code will come here -----------------
#access the values of the clicked buttons here...
return redirect(index)
else:
return render(request, 'index.html')
【问题讨论】:
标签: python django forms twitter-bootstrap django-views