How to redirect urls?

Question:

This is my main urls.py:

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

I want that any url entered by user will redirect to shop.urls and find there
like if the user enters /index it will search index in shop.urls not in main urls.

My shop.urls:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index),
    path('index', views.index),
]

enter image description here

Asked By: Path Parakh

||

Answers:

In main urls:

just give route name.

urlpatterns = [
    path('shop/', include("shop.urls")),
]

And this is your shop urls:

urlpatterns = [
    path('index/', views.index),
]

After changing above code.

You can navigate like this in your browser:

localhost:8000/shop/index/

You will redirect to index page.

Answered By: Manoj Tolagekar

You need to add / at the end of route so:

urlpatterns = [
    path('', views.index),
    path('index/', views.index),
]

Then enter the requested url as http://127.0.0.1:8000/index/

Answered By: Sunderam Dubey
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.