Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make trailing slash optional in django

I have a django app. In the app I have a url that looks like:-

path('url/', views.appmain, name="main")

The problem is that the trailing slash is required. I want to make the slash optional and the url accessible both with or without the slash. I checked APPEND_SLASH but it doesn't work. What's the solution for it.

like image 776
Nawal Kishore Avatar asked Nov 30 '19 11:11

Nawal Kishore


1 Answers

You can use re_path instead of path for use regular expression in your url pattern. use ? sign in your url like this:

from django.urls import re_path
re_path(r'url/?$', views.appmain, name="main")

Note: question mark matches zero or one / in the url. it accepts both domain.com/url and domain.com/url/

like image 197
Mojtaba Kamyabi Avatar answered Sep 22 '22 10:09

Mojtaba Kamyabi