Hi I have a strange error that I’m not sure how to debug. So I have a project that uses Django 4.2 and is on 4.2.16 currently. I have setup my urls.py to use from django.conf.urls.i18n import i18n_patterns
and wrap the urlpatterns with i18n_patterns. I have also set settings.USE_I18N=True and settings.LANGUAGE_CODE=“en”. Now this has worked fine in development in my local environment. And when I visit localhost:8000/about it automatically redirects to localhost:8000/en/about/ . I deployed this to beta and it worked fine there too. The site redirected as expected. Now I wanted to change the user the beta site was running under, because I had initially set up the service under my user id, and I don’t want to do that going forward because my user has sudo permissions etc. So I created another user without sudo privileges and then I cloned the git repository in a directory owned by this user, copied over the configuration files (.env file which is read by settings.py) The only change I made is in the .env file, where I pointed the STATIC_ROOT and MEDIA_ROOT to the paths for this new user. Similarly I changed the nginx site.conf file to point to this new path. And supervisord to point to this new path as well. But other than that, the configurations and code are identical. So the only difference is one user has sudo privileges, and the other user does not. But when the server runs under this user, redirects stop working. If I visit beta .mysite. org/about it doesn’t redirect to beta .mysite .org/en/about/ . The site loads fine if i manually enter the correct url - beta .mysite .org/en/about/ but the redirects do not work. I have double and triple checked that both directories are at the same git commit. I have even run a diff between the directories, and the only difference is in the .env file with the differences I noted above, about the STATIC_ROOT and MEDIA_ROOT paths. Both of them are connecting to the same database as well.
I’ve even done a diff against the django library installed in both venv directories, even though they both are on the explicitly specified 4.2.16 version.
Now I’m about to start placing logger.debug statements inside the django library functions to see why one django code is working differently than another. I can’t seem to think of anything else that might be the issue. Could someone point me to what might be the problem? Or has anyone seen a problem like this?
Thanks in advance,
arun
Ok to debug the issue, I setup django debug toolbar and enabled DEBUG=True on the server. And as soon as the server runs with DEBUG=True, and with django-debug-toolbar installed, it runs correctly. Redirects work. But the moment I turn off DEBUG, it stops working. The only thing I have in my urls.py that checks for DEBUG is:
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns = debug_toolbar_urls() + urlpatterns
The static url patterns will never be hit because the static files are served through nginx, which acts as the reverse proxy to this gunicorn server running this python project.
Ok so taking things out one by one, the issue seems to happen from the custom error handler for 404 errors. In my custom handler I was returning render(request, "error/404.html", {})
. Adding status=404
to the render function, fixed this issue.