0

I am trying to pass a parameter to a url before my include statement. I think my problem should be similar to this one, but I can't figure out why mine isn't working. I get the error NoReverseMatch at /womens/ Reverse for 'shampoo' with arguments '('womens',)' not found. 1 pattern(s) tried: ['(?P<gender>womens|mens)/items/shampoo/$']

*config.urls*
urlpatterns = [
    re_path(r'(?P<gender>womens|mens)/items/$, include('items.urls')),
    ...
]
*items.urls*
urlpatterns = [
    path('shampoo', views.shampoo, name='shampoo'),
    path('soap', views.soap, name='soap'),
    ...
]
{% url 'items:shampoo' gender='male' %}

I expect to get male/items/shampoo, but it doesn't work. Despite this, I can still manually go to male/items/shampoo.

1 Answer 1

0

This might work when you do it manually like male/items/shampoo, but as it is giving a NoReverseMatch error, Django is not able to find a matching URL pattern that you provided which included the parameters.

After looking into this for some time, something like this might be happening when you use {% url 'items:shampoo' gender='male' %} Assuming you are running this on your localhost, you expect the URL to look like male/items/shampoo This url says, look into male section, among items show shampoo. But you are passing the parameter in the wrong place. {% url 'items:shampoo' gender='male' %} means that the parameter male is passed to a url which has a namespace of shampoo. The URL with the namespace of shampoo does not accept any parameters.

Maybe try changing your code as follows.

*config.urls*
urlpatterns = [
    re_path(r'^$', include('items.urls')),
    ...
]
from django.conf.urls import url, path
*items.urls*
urlpatterns = [
    url(r'^(?P<gender>\w+)/items/shampoo/$', views.shampoo, name='shampoo'),
    path('soap', views.soap, name='soap'),
    ...
]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.