# django__django-12983
# Loki Mode Multi-Agent Patch
# Attempts: 1

--- a/django/utils/text.py
+++ b/django/utils/text.py
@@ -393,11 +393,13 @@ def slugify(value, allow_unicode=False):
     Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
     Remove characters that aren't alphanumerics, underscores, or hyphens.
     Convert to lowercase. Also strip leading and trailing whitespace.
+    Strip leading and trailing dashes and underscores.
     """
     value = str(value)
     if allow_unicode:
         value = unicodedata.normalize('NFKC', value)
     else:
         value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
     value = re.sub(r'[^\w\s-]', '', value.lower()).strip()
-    return re.sub(r'[-\s]+', '-', value)
+    value = re.sub(r'[-\s]+', '-', value)
+    return value.strip('-_')