-1

I'm new to django. I've tried the tutorial so many times. I now have started my own project until I am faced with NoReverseMatch ... and keyword arguments '{}' not found. 0 pattern(s) tried: []

my template 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)

Browser Output: NoReverseMatch at /kapra/Administration

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 if I don't use {% url %} and use {/kapra/...} format it works.

sandish
  • 39
  • 1
  • 6
  • section_listing is expecting only 1 keyword it seems. – cdvv7788 Oct 31 '15 at 08:52
  • Sorry, I posted irrevalent view at first, its edited now. The view section_listings is not being matched actually. – sandish Oct 31 '15 at 08:55
  • You are trying to solve an url that does not exist. maybe try {% url 'employee_listings' hsection section %} – cdvv7788 Oct 31 '15 at 08:58
  • Or update your url for section_listing to receive 2 keywords, not 1. – cdvv7788 Oct 31 '15 at 08:59
  • I'm sorry for the error. I actually tried {% url 'employee_listings' hsection section %} the error is Reverse for 'employee_listings' with arguments '(u'Administration', u'Employee Admin')' and keyword arguments '{}' not found. 0 pattern(s) tried: [] – sandish Oct 31 '15 at 09:06
  • Do you really mean to be passing `(u'Administration', u'Employee Admin')` to `{% url %}`? Is the space intended? You expect the URL to be `/Administration/Employee Admin/`? – Mike Covington Oct 31 '15 at 09:13
  • yes, indeed. the NoReverseMatch error comes in every other views as well, the other format though works everywhere. I've tried the namespace, 'kapra:employee_listings' as namespace in project urls.py is 'kapra'. Its the problem with matching reverse – sandish Oct 31 '15 at 09:16
  • Does my answer work now? – Sebastian Wozny Oct 31 '15 at 14:07
  • I'm afraid not. I'm reviewing my code again. – sandish Nov 01 '15 at 12:20

2 Answers2

0

You are passing two arguments besides the URL name to {% url ... %}:

{% url 'section_listings' hsection section %}

However, both your urls.py and views.py suggest you should only pass one extra argument:

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

Try this instead in your template:

{% url 'section_listings' hsection %}

or, since you have a named argument in your url, you can use:

{% url 'section_listings' hsection=hsection %}
Mike Covington
  • 2,147
  • 1
  • 16
  • 26
0

You are missing one slash at the end of the URL you are calling:

/kapra/Administration

should be

/kapra/Administration/

Consider manipulating your pattern to allow to omit the slash:

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

Note the ? after the last slash According to the documentation for url,you are passing args when you should pass kwargs. Try passing kwargs:

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

Explanation:

You may pass args to a pattern like this:

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

But only kwargs to this:

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

You cannot mix kwargs and args in one {% 'url' %} call.

Sebastian Wozny
  • 16,943
  • 7
  • 52
  • 69
  • Reverse for 'employee_listings' with arguments '()' and keyword arguments '{u'section': u'Employee Admin', u'hsection': u'Administration'}' not found. 0 pattern(s) tried: [] didn't work either. – sandish Oct 31 '15 at 09:23
  • Are you sure? I interpreted that as not being able to mix it like this: `{% url 'employee_listings' hsection=hsection section %}`. Because in 1.8, I pass an argument to a url that is defined w/ named variables and it works just fine. – Mike Covington Oct 31 '15 at 09:25
  • I'm sure, I did not mix it. I'm in Django 1.6.11 – sandish Oct 31 '15 at 09:42