18👍
def remove_lead_and_trail_slash(s):
if s.startswith('/'):
s = s[1:]
if s.endswith('/'):
s = s[:-1]
return s
Unlike str.strip()
, this is guaranteed to remove at most one of the slashes on each side.
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-Iterating over related objects in Django: loop over query set or use one-liner select_related (or prefetch_related)
- [Django]-How can I serialize a queryset from an unrelated model as a nested serializer?
8👍
Another one with regular expressions:
>>> import re
>>> s = "/get/category"
>>> re.sub("^/|/$", "", s)
'get/category'
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
- [Django]-Is there a naming convention for Django apps
- [Django]-Django Cannot set values on a ManyToManyField which specifies an intermediary model. Use Manager instead
Source:stackexchange.com