Issue
I want to add a prefix to all my URLs in Django python (3.0.3) so all my path that I defined in urls.py will start with "api/". The prefix may be changed in the future so I want it to be dynamic as possible
Solution
you can simply add prefix from MyProject/MyProject/urls.py
with include
function like this:
from django.urls import path, include
urlpatterns = [
# ...
# ↓this↓ is app name ↓this↓ is a namespace you can refer from your templates like this ex. {% url 'myapp:search' %}
path('api/', include(('myapp.urls', 'myapp'), namespace='myapp'))
]
and in your MyProject/myapp/urls.py
(you dont have this file when creating your project, you can create this file)
from .views import Signup,Search
urlpatterns = [
path('signup/', Signup.as_view(), name="signup"),
path('search/', Search.as_view(), name="search"),
]
so every view in your myapp
app will have api/
prefix in their urls like api/search/
or api/signup/
and as @kyore mentioned you can read official docs for more information.
Answered By - Ebrahim Karimi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.