0

urls.py


urlpatterns = [


    re_path(r'^([-\w]{3,})/$', shop_from_url, name='shop_from_url'),


    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('', include('products.urls')),
    path('authenticate/', include('authenticate.urls')),
    path('order/', include('order.urls')),
    path('offers/', include('offers.urls')),
    path('event/', include('event.urls')),

    path('product_upload/', include('product_upload.urls')),

    path('restaurant/', include('restaurant.urls')),

    path('shop/', include('store.urls')),

]

i have these url pattern. If you watch it, the first and other url path, they are exactly same in the way they are written. the difference is only that first url is parameterised url and other are hardcoded urls.

But actually when i call any of the url the first urls also called.

I have to check if there is any existing url (like authenticate/, product_upload/) matching to any of the app url then it should redirect to that otherwise it should call re_path(r'^([-\w]{3,})/$')(first url) this url

Is there any way to call the first url (which lies in this pattern) only when the url does not matches the other urls.

Note: I cannot change the urls as it is a requirement.

sandeepnegi
  • 123
  • 13
  • Share the url that you are calling e.g the URL coming from the browser. – Mohamed ElKalioby Feb 15 '22 at 08:31
  • it is a paramterised url so it could be anything like```http://127.0.0.1:8000/cronium``` . the thing is after the domain name i want to enter the store name and it should redirect me to that store. but because my other urls refer to the same pattern. when i send a reequest from other urls it also send request to the first url . which is a parameterised url. – sandeepnegi Feb 15 '22 at 08:37

1 Answers1

1

You should put your urls in the logic check order. According Django documentation (https://docs.djangoproject.com/en/4.0/topics/http/urls/#how-django-processes-a-request):

Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.

urlpatterns = [
    path('ckeditor/', include('ckeditor_uploader.urls')),
    path('authenticate/', include('authenticate.urls')),
    path('order/', include('order.urls')),
    path('offers/', include('offers.urls')),
    path('event/', include('event.urls')),
    path('product_upload/', include('product_upload.urls')),
    path('restaurant/', include('restaurant.urls')),
    path('shop/', include('store.urls')),


    re_path(r'^([-\w]{3,})/$', shop_from_url, name='shop_from_url'),
    path('', include('products.urls'))
]
Tonio
  • 1,642
  • 1
  • 4
  • 10
  • i don't know whether this will work or not. But this is not what i want. because i'm not the only one who is working on this project and i don't expect from others to remember to put there new app url in a paritcular order, which harldy anyone does. – sandeepnegi Feb 15 '22 at 09:16
  • @sandeepnegi, a mason must lay the bricks in a certain order or the wall will fall. Why shouldn't a programmer know how to code in a certain order? – Tonio Feb 15 '22 at 09:26
  • This is the way Django works, the first URL that will match the regex of the URL will be called. – Mohamed ElKalioby Feb 15 '22 at 11:41
  • @Tonio i understand that but i'm looking for a solution which can be used to without keeping the order as well – sandeepnegi Feb 15 '22 at 12:00