我有一个Django查询,其中将以月作为输入并显示带有查询数据的pdf文件。
element = request.GET.get('q')
query=employeeModel.objects.filter(month__contains = element,username = request.user).values_list()
这是该查询的模板
<h1>Employee PaySlip Generation</h1>
<h5>Hello {{request.user}}</h5>
<form method="GET" action="">
<span><input type="text" name='q' placeholder="Search: Month" value="{{ request.GET.get }}"/></span>
<span><input class="btn btn-primary btn-sm" type="submit" value="Search" >
</form>
我不知道该怎么做,该怎么做,按钮不仅会向查询提交键入的值,还会调用pdf_generate函数。
修改
utils.py
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
从utils.py导入reder_to_pdf
views.py
def user_pdf(request):
template = get_template('payslip.html')
element = request.GET.get('q')
print(element)
query = employeeModel.objects.values_list().filter(username= request.user,month__contains = element )[0]
name = query[1]
emp_id = query[2]
context = {'query_name':name, 'query_id':emp_id}
html = template.render(context)
pdf = render_to_pdf('payslip.html', context)
return HttpResponse(pdf, content_type='application/pdf')