从 html 页面 url 传递参数映射到 django 中的视图
passing a parameter from html page url mapping to the views in django
front.html
<div class="row">
<a href="{% url 'emp_pay_list' Information_technology %}">
<div class="col-md-6 col-sm-6 col-lg-3">
<div class="dash-widget clearfix card-box" style="height: 200px;">
<span class="dash-widget-icon"><i class="fa fa-cubes" aria-hidden="true"></i></span>
<div class="dash-widget-info">
<h3>20</h3>
<span>Information Technology</span>
</div>
</div>
</div>
</a>
<a href="{% url 'emp_pay_list' sales %}">
<div class="col-md-6 col-sm-6 col-lg-3">
<div class="dash-widget clearfix card-box" style="height: 200px;">
<span class="dash-widget-icon"><i class="fa fa-users" aria-hidden="true"></i></span>
<div class="dash-widget-info">
<h3>7</h3>
<span>Sales Team</span>
</div>
</div>
</div>
</a>
</div>
上面的代码是前端部分 HTML 这里我想通过单击一个框将参数传递到 urls.py 并且取决于我们传递的参数应该获取数据。
urls.py
url(r'^list_of_employees/<string>/$', views.Pay_slip_list , name='emp_pay_list'),
这是我在 urls.py 中用来传递参数的代码。
views.py
def Pay_slip_list(request, department):
hello = employees.objects.select_related('employee_name', 'employee_auth').filter(department=department)
return render(request, 'hr/emp_pay_list.html', {'hello': hello})
这是 views.py 部分,我根据单击该框后传递的 string/parameter 从数据库中获取数据。
您的 URL 模式必须类似于
from django.urls import path, re_path
path('list_of_employees/<str:department>/', views.Pay_slip_list , name='emp_pay_list'),
或使用re_path
re_path(r'^list_of_employees/(?P<department>[\w\-]+)/$', views.Pay_slip_list , name='emp_pay_list')
use path
instead of url
for Django version >= 2.0, or use re_path
for complex patterns, re_path
is same as url
method in lower versions
front.html
<div class="row">
<a href="{% url 'emp_pay_list' Information_technology %}">
<div class="col-md-6 col-sm-6 col-lg-3">
<div class="dash-widget clearfix card-box" style="height: 200px;">
<span class="dash-widget-icon"><i class="fa fa-cubes" aria-hidden="true"></i></span>
<div class="dash-widget-info">
<h3>20</h3>
<span>Information Technology</span>
</div>
</div>
</div>
</a>
<a href="{% url 'emp_pay_list' sales %}">
<div class="col-md-6 col-sm-6 col-lg-3">
<div class="dash-widget clearfix card-box" style="height: 200px;">
<span class="dash-widget-icon"><i class="fa fa-users" aria-hidden="true"></i></span>
<div class="dash-widget-info">
<h3>7</h3>
<span>Sales Team</span>
</div>
</div>
</div>
</a>
</div>
上面的代码是前端部分 HTML 这里我想通过单击一个框将参数传递到 urls.py 并且取决于我们传递的参数应该获取数据。
urls.py
url(r'^list_of_employees/<string>/$', views.Pay_slip_list , name='emp_pay_list'),
这是我在 urls.py 中用来传递参数的代码。
views.py
def Pay_slip_list(request, department):
hello = employees.objects.select_related('employee_name', 'employee_auth').filter(department=department)
return render(request, 'hr/emp_pay_list.html', {'hello': hello})
这是 views.py 部分,我根据单击该框后传递的 string/parameter 从数据库中获取数据。
您的 URL 模式必须类似于
from django.urls import path, re_path
path('list_of_employees/<str:department>/', views.Pay_slip_list , name='emp_pay_list'),
或使用re_path
re_path(r'^list_of_employees/(?P<department>[\w\-]+)/$', views.Pay_slip_list , name='emp_pay_list')
use
path
instead ofurl
for Django version >= 2.0, or usere_path
for complex patterns,re_path
is same asurl
method in lower versions