我有这个问题:我想创建一个模式来从外键中获取相关对象。模态显示,呈现表单并列出结果。搜索按钮关闭模式,我不知道如何用单选框拾取对象。我的代码如下:
pay_form.html
<div class="form-inline">
<label for="idticket" style="margin-right: 5px">Ticket:</label>
{{ payForm.idticket }}
<a data-toggle="modal" href="searchTicket" type="button" class="btn btn-default btn-xs">
<i class="icon-search"></i>
</a>
</div>
<a href="/payments/cash" class="btn btn-default">Cancel</a><input type="submit" class="btn btn-primary" value="Save"/>
</form>
<div id="searchTicket" class="modal fade" style="display: none">
{% include "payments/ticket_search.html" %}
</div><!-- /.modal -->
search_ticket.html
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Selecct Ticket</h4>
</div>
<div class="modal-body">
<form action="" method="get"/>
<fieldset>
<div class="form-inline">
<label>Ticket:</label>
<input type="text" name="idticket"/>
</div>
<input type="submit" class="btn btn-info" value="Search"/>
</fieldset>
</form>
<table class="table table-striped table-condensed">
<thead>
<tr>
<th>TICKET</th>
<th>DATE</th>
<th>AMOUNT</th>
</tr>
</thead>
<tbody>
{% for result in results %}
<tr>
<th><input type="radio" value="{{result.ticket}}"></th>
<th>{{result.idticket}}</th>
<th>{{result.date}}</th>
<th>{{result.amount}}</th>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Selecct</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
视图.py
def ticket_search(request):
if request.method == 'GET' and 'idticket' in request.GET:
sForm = SearchTicketForm(request.GET)
if sForm.is_valid():
idticketF = sForm.cleaned_data.get('idticket')
qs = Ticket.objects.all()
if idticketF is not None:
qs = qs.filter(idticket__icontains=idticketF)
qs = qs.order_by('idticket',)
else:
qs = Ticket.objects.all().order_by('idticket',)
else:
sForm = SearchTicketForm()
qs = Ticket.objects.all().order_by('idticket',)
return render(request, 'payments/ticket_search.html', {'sForm':sForm,'results':qs})
所以有两件事:
1)在模态窗口中执行搜索。
2) 返回选中的值。