您好,我是Django / python的新手-我遇到一种情况,我正在寻求一些定向指导/建议,因此我有一个带有搜索框vaule = rep_date
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
</head>
<body>
<div class="row">
<div class="col-sm-2">
<h1 style="text-align:center;position:relative">MENU</h1>
<nav class="navbar bg-ligh">
<form method="post" enctype="multipart/form-data" action="">
{% csrf_token %}
<input type="text" class="form-control form-control-sm" value="rep_date">
<input type="submit" value="ok">
</form>
<ul class="navbar-nav">
<li class="nav-items"><a href="" class="nav-link">Report 1</a></li>
<li class="nav-items"><a href="#" class="nav-link">Report 2</a></li>
<li class="nav-items"><a href="#" class="nav-link">Report 3</a></li>
<li class="nav-items"><a href="#" class="nav-link">Report 4</a></li>
<li class="nav-items"><a href="#" class="nav-link">Report 5</a></li>
</ul>
</nav>
</div>
<div class="col-sm-10">
{% block content %}
{% endblock %}
</div>
</div>
</body>
</html>
提交文本框后,我试图使用导航栏的返回值来返回每个报表的相应视图,使用发布的文本框的输入值输入视图的最终视图结果以呈现每个html页面< / p>
每个报告的视图样本:
def ReportOne(request):
RepOneResult = Reporttable.objects.filter(yyyy_mm=PostedVal).order_by("-approved")[:25]
return render(request, "reports/RepOne.html", {'RepOneResult': RepOneResult})
因此,在应用代码逻辑以及在每个视图中包含张贴在value = rep_val
上的表单时,我遇到了问题
答案 0 :(得分:0)
我认为您可以做到的几种方法,
1-您可以为每个视图创建一个URL模式,并根据单击的报告将其传递到相应的视图。看起来像这样,
<form id="reportForm" method="post" enctype="multipart/form-data" action="">
{% csrf_token %}
<input type="text" class="form-control form-control-sm" value="rep_date">
<input type="submit" value="ok">
</form>
<a class='report-link' href='#' data-action='{% url "report_one" %}'>Report 1</a>
<a class='report-link' href='#' data-action='{% url "report_two" %}'>Report 2</a>
<a class='report-link' href='#' data-action='{% url "report_three" %}'>Report 3</a>
和一些js / jquery
$('.report-link').click(fucntion() {
$('#reportForm').attr('action', $(this).attr('data-action'))
$('#reportForm').submit()
})
2-您也可以通过为报告创建一个单一视图来实现这一目标,
<form id="reportForm" method="post" enctype="multipart/form-data" action="">
{% csrf_token %}
<input type="text" class="form-control form-control-sm" value="rep_date">
<input type="submit" value="ok">
</form>
<a class='report-link' href='#' data-action='{% url "reports" %}?report=report_one'>Report 1</a>
<a class='report-link' href='#' data-action='{% url "reports" %}?report=report_two'>Report 2</a>
<a class='report-link' href='#' data-action='{% url "reports" %}?report=report_three'>Report 3</a>
然后在报告视图中,您只需检查GET参数并相应地生成报告即可,
def reports(request):
if request.method == 'POST':
if 'report' in request.GET and request.GET['report'] == 'report_one'
generate_report_1()
elif 'report' in request.GET and request.GET['report'] == 'report_two'
generate_report_2()
elif 'report' in request.GET and request.GET['report'] == 'report_three'
generate_report_3()
# ... and so on
我之所以选择第二种方法,是因为我不需要为我创建的每个报告添加URL模式,这会使事情变得更加动态。
希望这会有所帮助!