我正在尝试使用URL模式匹配来动态显示模板中的数据。目标是根据我拥有的组织类型显示数据:3:制造商,供应商和分销商。
因此,如果URL匹配/profiles/man_dash/**manufacturers**/
,则显示所有制造商的数据。下面是我的代码:
查看
def man_org_list(request, member_type=None):
member_type_map = {
'manufacturers': 'Manufacturer',
'suppliers': 'Supplier',
'distributors': 'Distributor'
}
member_type = member_type_map.get(member_type, None)
if member_type is None:
raise Http404
queryset = Organization.objects.filter(member__member_flag=1, member__member_type=member_type).order_by('id')
return render(request, 'profiles/man_dash.html', {'object_list': queryset})
核心urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('profiles/', include('profiles.urls'))
]
urls.py
urlpatterns = [
url(r'^$', views.org_list, name='org_list'),
url(r'^(?P<id>\d+)/$', views.org_details, name='org_details'),
url(r'^man_dash/<str:member_type>/', views.man_org_list, name='man_org_list')
]
我要显示的代码块:
{% for org in object_list %}
<tr>
<th scope="row">{{ org.id }}</th>
<td>{{ org.org_name }}</td>
<td>{{ org.org_type }}</td>
{% for member in org.member.all %}
<td>{{ member.member_flag }}</td>
{% endfor %}
{% for c_score in org.c_score.all %}
<td>{{ c_score.completeness_score }}%</td>
{% endfor %}
<td><a href="{% url 'org_details' org.id %}" target="_blank">View</a></td>
</tr>
{% endfor %}
由于某种原因,我不断收到错误消息:
The current path, profiles/man_dash/manufacturers/, didn't match any of these.
答案 0 :(得分:3)
url(r'^man_dash/<str:member_type>/', views.man_org_list, name='man_org_list')
您在这里混淆了旧的和新的URL语法。 <str:member_type>
属于path()
,但是r'^...'
是属于url()
的正则表达式。
尝试将其更改为:
path('man_dash/<str:member_type>/', views.man_org_list, name='man_org_list')