4 关键字搜索
视图
from django.shortcuts import render
from django.db.models import Q
from utils.pager import Pagination
from web import models
def transaction_list(request):
keyword = request.GET.get('keyword', '').strip()
con = Q()
if keyword:
con.connector = 'OR'
con.children.append(('order_oid__contains', keyword))
queryset = models.TransactionRecord.objects.filter(con).filter(customer_id=request.nb_user.id).order_by('-id')
pager = Pagination(request, queryset)
context = {
'pager': pager,
'keyword': keyword,
}
return render(request, 'transaction_list.html', context)
前端(bootstrap):
<div class="clearfix" style="margin-bottom: 5px">
<div class="right">
<form class="form-inline" METHOD="get">
<div class="form-group">
<input name="keyword" type="text" class="form-control" placeholder="请输入关键字" value="{{ keyword }}">
</div>
<button type="submit" class="btn btn-default">
<span class="glyphicon glyphicon-search"></span>
</button>
</form>
</div>
</div>