Django template throws NoReverseMatch error

djangodjango-templatesdjango-views

I had two methods create and update in the views, in which update takes one argument whereas create does not take any. I have decided to turn them into only one function update_create because they were not that different.

This is how the new method in views looks:

  def update_create(request, id=None):

This is my urls.py:

  url(r'^(\d+)/update/$|create/$', update_create, name='update_create'),

This is how my template looks in templates/list.html

  <a href="{% url 'update_create' %}"> Create a new event </a>

I got this error when using the above code:

  NoReverseMatch at /agenda/list/
  Reverse for 'update_create' with arguments '()' and keyword arguments '{}' not found.

But, if I use this in my templates instead (I have added an argument), it works without any errors:

  <a href="{% url 'update_create' 1 %}"> Create a new event </a>

Can someone explain what's happening? Why previous code didn't work, and Why the new code is working?

Best Answer

URL pattern (\d+) expects number to be provided as argument. To resolve the issue simply provide urls like this:

url(r'^(\d+)/update/$', update_create, name='update_create'),
url(r'^update/$', update_create, name='update_create'),
Related Topic