Home > database >  Django - The current url didn’t match any of the these
Django - The current url didn’t match any of the these

Time:04-17

I have Django 4.0.4 I tried the following url : http://127.0.0.1:8000/cp2/sbwsec/1076/o2m/section/wseclmts/47/p/element/sbwblmnt/1077/ but it gives me error Page not found (404)

The current path, cp2/sbwsec/1076/o2m/section/wseclmts/47/p/element/sbwblmnt/1077/, didn’t match any of these.

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

I have 1370 patterns - where the correct pattern is on the line 268 as you can see from the debug exception page

cp2/ (?P<parent_element_name>\w )/(?P<parent_id>\d )/p/(?P<parent_field>\w )/sbwblmnt/(?P<item_id>\d )/$ [name='sbwblmnt_epec']

Thank you for the help

CodePudding user response:

It looks like you wrote a relative URL in the template, so something like:

<a href="wseclmts/47/p/element/sbwblmnt/1077/">link</a>

This will append the path wseclmts/47/p/element/sbwblmnt/1077/ to the already existing path, so if you browse this when the browser is visiting the /cp2/sbwsec/1076/o2m/section/ path, it will visit /cp2/sbwsec/1076/o2m/section/wseclmts/47/p/element/sbwblmnt/1077/ path. This thus results in long URLs (that might even no longer fit in the 2048 character limit.

You should work with an absolute path, like:

<a href="/cp2/wseclmts/47/p/element/sbwblmnt/1077/">link</a>

so with a leading slash. This will let the browser visit the /cp2/wseclmts/47/p/element/sbwblmnt/1077/ path. You can work with the {% url … %} template tag [Django-doc] to construct the path, so:

<a href="{% url 'sbwblmnt_epec' 'wseclmts' 47 'element' 1077 %}">link</a>

Where the first parameter 'sbwblmnt_epec' is the name of the view, and the next parameters are values for the parameters. This will look to the patterns and construct the URL to visit the given path with the given parameters.

  • Related