NoReverseMatch ...和找不到关键字参数“{}”。尝试过0种模式:[]

时间:2015-10-31 08:46:16

标签: python django templating

我是django的新手。我已经多次尝试过这个教程了。我现在已经开始了自己的项目,直到我遇到NoReverseMatch ...并且找不到关键字参数“{}”。尝试了0种模式:[]

我的模板 section_list.html

{% for section in section_name %}
    <a href='{% url 'employee_listings' hsection section %}' >
       {{ section }}

 </br>
{% endfor %}

urls.py

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^letters/(?P<employee_id>\d+)$', views.letters, name='letters'),    
    url(r'(?P<word>\d+)/$', views.headsection_listings, name='headsection_listings'),
    url(r'(?P<hsection>\w+)$', views.section_listings, name='section_listings'),
    url(r'(?P<hsection>\w+)/(?P<section>\w+)/$', views.employee_listings, name='employee_listings'),
    )

views.py

def employee_listings(request, hsection, section):
    #import pdb; pdb.set_trace()
    employee_list = Employee.objects.all()
    employee_id = [p.emp_id for p in employee_list if p.section.section_name == section]
    employee_name = [p.emp_first_name +' ' +  p.emp_last_name for p in employee_list if p.section.section_name == section]
    #    import pdb; pdb.set_trace()
    context = {'employee_name':employee_name, 'section':section, 'employee_id':employee_id}
    return render(request, 'kapra/employee_list.html', context)

def section_listings(request, hsection):
    section_list = Section.objects.all()
#    import pdb; pdb.set_trace()
    section_name = [p.section_name for p in section_list if p.headsection.head_section_name == hsection]
    context = {'section_name':section_name, 'hsection':hsection}
    return render(request, 'kapra/section_list.html', context)

浏览器输出:     在/ kapra / Administration

的NoReverseMatch
Reverse for 'section_listings' with arguments '(u'Administration', u'Employee Admin')' and keyword arguments '{}' not found. 0 pattern(s) tried: []
.... Reverse for 'section_listings' with arguments '(u'Administration', u'Employee Admin')' and keyword arguments '{}' not found. 0 pattern(s) tried:[]

django_version 1.6.11 如果我不使用{%url%}并使用{/ kapra / ...}格式,则可以使用。

2 个答案:

答案 0 :(得分:0)

您将除URL名称之外的两个参数传递给{% url ... %}

{% url 'section_listings' hsection section %}

但是,urls.pyviews.py建议您只应传递一个额外的参数:

# urls.py
url(r'(?P<hsection>\w+)$', views.section_listings, name='section_listings'),
# views.py:
def section_listings(request, section):

请在模板中尝试此操作:

{% url 'section_listings' hsection %}

或者,由于您的网址中有命名参数,因此您可以使用:

{% url 'section_listings' hsection=hsection %}

答案 1 :(得分:0)

您正在呼叫的网址末尾缺少一个斜杠:

/kapra/Administration

应该是

/kapra/Administration/

考虑操纵你的模式以允许省略斜杠:

url(r'(?P<hsection>\w+)/(?P<section>\w+)/?$', views.employee_listings, name='employee_listings'),

请注意最后一个斜杠后的? 根据{{​​1}}的{​​{3}},当您通过url时,您将通过args。尝试传递kwargs

kwargs

<强>解释

你可以将args传递给这样的模式:

{% for section in section_name %}
    <a href='{% url 'employee_listings' hsection=hsection section=section %}' >
       {{ section }}
     </br>
{% endfor %}

但只有kwargs:

url(r'^client/([0-9]+)/$', app_views.client, name='app-views-client')

您无法在一次 url(r'(?P<hsection>\w+)/(?P<section>\w+)/$', views.employee_listings, name='employee_listings'), 电话中混合使用kwargs和args