Catching an AI-Generated Bug in Django URL Handling
Recently, I was experimenting with Django URL customization and token-based encryption. I used AI "ChatGPT" to optimize code include these lines :
url = url[1:] + req.request_token
url = '/' + encrypt_text(url) + '/'
But I spotted a subtle bug in the output.
url = url.rstrip("/") # ❌ Wrong: strips slashes from the right
url = "/" + url
The issue: rstrip("/") removes trailing slashes from the end of the string, but Django URLs require a single leading slash at the start. This could accidentally produce malformed URLs such as:
"//myview/12345/token/"
💡 Lesson learned: even with AI assistance and easy code hacking, you always need to review and validate code carefully. Small string operations can break URL patterns in Django.
Using AI to optimize my code.