how-to django-error   1   2572
Solving Django error 'NoReverseMatch at' URL with arguments '()' and keyword arguments '{}' not found.

Every Django developer encounters below error at least once in their life for sure.

NoReverseMatch at /url_path/ Reverse for 'url_name' with arguments '()' and keyword arguments '{}' not found. n pattern(s) tried: []


Beginners spend many hours debugging the issue, jumping from question to question on Stackoverflow and posting in multiple groups on Facebook. In this article we have tried to list all the common mistakes developer makes which leads them to above error.


What is this error:

First lets see what this error is trying to tell developer.

Django is trying to find a URL with name 'url_name' and it is not able to find any pattern match.

Application traverses the urls.py file from top to bottom and match the pattern. It stops at first successful match.

Arguments '()' and keyword arguments '{}' means that the URL application is searching for have no arguments or keyword arguments in it.

Application tried to match against 'n' patterns.



Why this error:

Lets say we have below urls.py file.

from django.urls import path

from . import views

app_name = 'polls'
urlpatterns = [
    #...
    path('articles/<int:year>/', views.year_archive, name='news-year-archive'),
    #...
]


In above configuration:

  • URL path is articles/<int:year>/ , like /articles/2018/
  • Function name in views.py  file is year_archive
  • URL name is news-year-archive


Main reasons behind this error are:

1) Using wrong URL name in template html file.

For example in below template code snippet,

<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>
instead of using news-year-archive are you using news-year-archives  


2) Commented HTML code.

Django tries to parse the template tags in commented HTML code as well while trying to render the template.

If all the url template tags in your template are correct, check the commented part as well.

If there is any error in commented part, remove it.

<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>
<!-- <a href="{% url 'news-year-archives' 2012 %}">2012 Archive</a> -->
In the snippet above, application will throw the error for second line as there is no URL with name 'news-year-archives '.  


3) Arguments or Keyword arguments

Make sure you are passing the correct number of Arguments or Keyword arguments in URL template tag.

As you can see in urls.py file, URL with name 'news-year-archive ' expects an argument 'year' in it. If you are not passing the year in url template tag, application will throw the error. 

So

<a href="{% url 'news-year-archive' %}">2012 Archive</a>


should be

<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a>


4) Including app URL conf in project urls.py

Make sure you have included the app urls in project urls.py file.

from django.urls import include, path
from django.contrib import admin

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app_name/', include('app_name.urls', namespace="app_name")),
]

5) Namespace

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names.

For example app1 have URL name 'contacts', now app2 can also have URL with same name provided we are using namespace while including URLs in project's urls.py file.

In the template snippet inside url  template tag, we are using namespace:url_name syntax

<a href="{% url 'app_name:news-year-archive' 2012 %}">2012 Archive</a>


6) Incorrect RegEx

If you are using any regular pattern in your URLs, make sure it is correct. In below URL page_number should consist only digits (\d+).

re_path(r'comments/(?:page-(?P<page_number>\d+)/)?$', comments)


7) Django Version

In Django 1.10, the ability to reverse a URL by its python path was removed. The named path should be used instead.  


If your application is still throwing this error, comment here and we will help you sail through this.  


Host your Django app for free.  


References:
[1] https://stackoverflow.com/
[2] https://docs.djangoproject.com/en/2.0/topics/http/urls/
how-to django-error   1   2572

Related Articles:
Python Script 3: Validate, format and Beautify JSON string Using Python
Validating json using python code, format and beautify json file using python, validate json file using python, how to validate, format and beautify json...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in DJango. Encrypting the critical information in Django App. Encrypting username, email and password in Django...
Adding Robots.txt file to Django Application
Adding robots.txt file in your Django application. Easiest way to add robots.txt file. Django application robots.txt file. Why should you add robots.txt file in your Django Application....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scrapping large amount of tweets within minutes using celery, RabbitMQ and docker cluster. Scraping huge data quickly using docker cluster with TOR....

1 thought on 'Solving Django Error 'Noreversematch At' Url With Arguments '()' And Keyword Arguments '{}' Not Found.'
Aditya Vikram Monga :
I was going out of my mind trying to find the error. Option 2 -> Django parses HTML comments too. I had an old not updated url in my template comments.Bless you, good person.
Admin :
Thanks Aditya. Glad we are helpful. Please share the blog and help us reach more people.

Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:






© pythoncircle.com 2018-2019