我有以下网址模式 -
url(r'^detail/(?P<var>[\w]+)/(?P<var_slug>[\w-])/$', 'player_detail', name='player_detail'),
在我看来,我有以下几点 -
model_dict = {"player":PlayerProfile, "event":PlayerEvent, "need":PlayerNeed}
def player_list(request, var=None, var_slug=None):
'''
displays the list of vars
'''
objs = model_dict.get(var).objects.filter(slug=var_slug).order_by('-creation_time')[:20]
template_name = "list_"+str(var)+"s.html"
return render(request, template_name, {"objs":objs})
在我的模板中,我最终执行以下操作 -
<a href="{% url 'player_detail' var='baseball' slug='obj.slug' %}">details of Player</a>
我得到的错误如下 -
Reverse for 'player_detail' with arguments '()' and keyword arguments '{u'var': u'baseball', u'slug': u'obj.slug'}' not found.
我错过了什么?
另外,根据参数中的变量动态选择模型并动态生成模板名称是否是一种好方法?