Home > Software engineering >  Django url mapping error - URL doesn't match error
Django url mapping error - URL doesn't match error

Time:10-23

HI I got an error when calling an endpoint from django url

Here is my piece of javascript

  window.location.assign(`customer/?id=1`}

My urls.py

  path('customer/(?P<int:id>\d )', views.get_customer, name="get_customer")

Here is how it is called in the views.py

  def get_customer(request, id):

       print(f"the id {id}")
       # # customer = get_object_or_404(Customer, pk=id)
       # # print(f"customer {customer}")

  

The Idea is to get the customer record filtered by id passed as query paramas But I got an error

     Using the URLconf defined in inventorymanagement.urls, Django tried these URL patterns, in this order:

     admin/
     [name='home']
     customer/(?P<int:id>\d ) [name='get_customer']

     The current path, customer/, didn’t match any of these.
    

Any idea on how to solve this?

CodePudding user response:

the path in urls.py does not match the url you set in windows.location... Try this:

window.location.assign('customer/1')

Some examples on Django documentation of using Regexp in url: https://docs.djangoproject.com/en/4.1/topics/http/urls/#using-regular-expressions

  • Related