How to change the url of an app in Django to custom url
Question:
I have an app in my Django project that called Stressz. So the url of my app is:
http://localhost:8000/stressz/
http://localhost:8000/stressz/siker
http://localhost:8000/stressz/attitud
How can I change the url without changing the name of the app from the above url to something like this:
http://localhost:8000/mpa
http://localhost:8000/mpa/siker
http://localhost:8000/mpa/attitud
urls.py
app_name = 'stressz'
urlpatterns = [
path('', views.index, name='index'),
path('siker', views.siker, name='siker'),
path('attitud', login_required(views.AttitudCreateView.as_view()), name='attitud_item'),
...
Answers:
Look into your root urls.py, which you can usually find in your project (not app) folder. There has to be a line similar to this:
urlpatterns = [
path('stressz', include('stressz.urls')),
]
If you then change it to:
urlpatterns = [
path('mpa', include('stressz.urls')),
]
It should work as you intended.
You can also avoid typing this name by redirection. Still you can change the name to "mpa" as explained by dustin-we.
from django.urls import path, include
from django.views.generic.base import RedirectView
urlpatterns = [
path('', RedirectView.as_view(pattern_name='stressz'), name='main'),
path('stressz/', include('stressz.urls')),
]
I have an app in my Django project that called Stressz. So the url of my app is:
http://localhost:8000/stressz/
http://localhost:8000/stressz/siker
http://localhost:8000/stressz/attitud
How can I change the url without changing the name of the app from the above url to something like this:
http://localhost:8000/mpa
http://localhost:8000/mpa/siker
http://localhost:8000/mpa/attitud
urls.py
app_name = 'stressz'
urlpatterns = [
path('', views.index, name='index'),
path('siker', views.siker, name='siker'),
path('attitud', login_required(views.AttitudCreateView.as_view()), name='attitud_item'),
...
Look into your root urls.py, which you can usually find in your project (not app) folder. There has to be a line similar to this:
urlpatterns = [
path('stressz', include('stressz.urls')),
]
If you then change it to:
urlpatterns = [
path('mpa', include('stressz.urls')),
]
It should work as you intended.
You can also avoid typing this name by redirection. Still you can change the name to "mpa" as explained by dustin-we.
from django.urls import path, include
from django.views.generic.base import RedirectView
urlpatterns = [
path('', RedirectView.as_view(pattern_name='stressz'), name='main'),
path('stressz/', include('stressz.urls')),
]