Django logging errors occuring in admin page - django

I face the following issue:
I want to add a new instance of a model in the django admin page, but I do get an 500 error. Now I want to log all the errors occurring in the admin page to a file.
Important details:
website running in production, hence I do not get feedback in the browser or the console (since I use Gunicorn).
this occurs only for one specific model.
this only happens when adding a new instance for that model, not when changing exisiting instance
logging in the view does not help, since error occurs within admin page
I do not want you to help me debug my django code. I am only interessted for tips and a tutorial on how to log every error occuring no matter if it happens in the admin page or the actual site. I am happy for any tips and suggestions! Thanks! Thanks for any advice!

To get the stack trace of an error with gunicorn as proxy server:
sudo systemctl status gunicorn
For the logging tutorial (answer by #markwalker_):
logging tutorial

Related

Django web site URLS gets "?next=" randomly after deployed to heroku

After deploying django app on heroku the url after every form submission or link in site been press resulting with "?next=" inserts into the URL address, I don't understand from where it comes and why heroku keep inserting them in a random way, running the application locally just working perfectly.
I've deleted the application from heroku and upload it again, the fault persists.
There's no http errors what so ever. If I keep submitting the form eventually will works.
For example: pressing the log button in the django admin console results with this URL: https://appname.herokuapp.com/admin/login/?next=/admin/login/admin/, hitting it quite a bit will finally make it work with the correct url display: https://mishnayot.herokuapp.com/admin/.
Switching from heroku postgres to AWS RDS postgres didn't help.
Help will be very appreciated.
When a user hits a page requiring authentication, they get redirected to the login URL with a ?next= indicating the page they were trying to access. This way, after they've logged in, they'll get the page they intended to access.
In your case, it seems like /admin/login/admin/ requires a login and the login is actually at /admin/login/. I'm guessing you have a bad link to /admin/login/admin/ somewhere when you intended to just have /admin/login/?
Well, struggling for days just to understand it's heroku backend issue and there's not enough information that could help me solving it (i did try to contact heroku support directly but I'm on a free account).
Finally I decided to deploy it using elastic beanstalk. The implementation took some time with long learning curve but now my site works and works great!
Thanks for trying to help Tim.
Cheers!

How do I get a Django Server Error 500 report

I recently ran my tests on django for my project, one of them turned out as a server error 500. Thi confused me as I thought everything was passing. I currently have DEBUG=True. When I checked the documentation it said to set debug to true and add some admins to email for the full output. Is there an easier way to get the output or should I work on setting that up.
For more info my project is still being developed. I dont really want to post my test code as I really need more debugging experience but if you people ask I will!
Thanks for any help!
Errors are mailed throught Django logging framework. You must configure logging apropiatly to use other Handler than Email (email is by default for errors on django). Change it to Console or File Handler instead....
Here is the source doc:
https://docs.djangoproject.com/en/3.0/topics/logging/

Django admin page login causing IIS to crash, need to restart iis everytime

My site is working fine. I can access all the pages. But when I am accessing admin page, it loads, prompts for username and password. After entering username and password when clicked login, it takes sometimes and after that I get below error:
This page isn’t working mytestapp.company.com didn’t send any data.
ERR_EMPTY_RESPONSE
After refreshing the page, I get below error:
Service Unavailable HTTP Error 503. The service is unavailable.
After this no page loads. After restarting IIS using cmd (iisreset /noforce) again when I try to refresh the homepage, I see I am logged in which means admin page logged me in but after that response did not come and it something went wrong on the server side which caused server to crash.
I am not sure how to proceed with this. Earlier my admin site use to work fine. No recent changes in code. The only change I did is I synced the DB from another DB which has more data.
I am using virtual env which has
python version is 2.7.3, Django version 1.3 in it
IIS version 7.5 on WindowsServer 2008R2 (Python IsAPIe handler)
Please help me on this. I am stuck with this issue...
Issue is resolved now. IIS service was getting stopped because of a series of error caused while rendering admin page. After checking windows event log and understanding error code, got to know that the Python-Runtime.dll which admin page was referring was having some issue. I replaced the dll with the previous working version and then everything started working fine.

django social auth error after completing pipeline

I am using django social auth to power facebook connect in my app (admittedly with a little complicated user model and legacy database).
I was redirected to error page and seem to be running into an error AFTER completion of pipeline( redirect as last step of pipeline still redirects me).
Can someone tell me where to look to debug this?
Thanks,
A good place to start would be to look in social-auth's views.py, at the few places where the redirect to LOGIN_ERROR_URL happens (the variable url is set to LOGIN_ERROR_URL and then HttpResponseRedirect(url) is called). Add some print statements, or better, set breakpoints using the python debugger. If you run your app in the Django development server, the print statements will show up in the terminal in which you ran the server. Otherwise, they may show up in your server logs, depending on your configuration. You may also find django-debug-toolbar helpful.
Using print statements or the debugger, my workflow would be:
Figure out what line in views.py the redirect is triggered from
Figure out what condition causes that line to be reached
Inspect the variables leading to that condition
Sorry this is so general. Happy to help more if you can provide some more specific information.
Aaron

What is an efficient way to view errors in Django?

I am trying to add a new middle ware into my Django application. However, my app now stalls when being visited. Eventually, after a long time, a 500 error surfaces.
Is there an error log anywhere on the server for Django? I want to see what the error was.
In general, logging will depend on how you are serving django (modwsgi, gunicorn, passenger, etc...), how you have configured django logging and where the error occurs, i.e. if the error happens before/after django is handling the request.
If you have configured your mail host and logging, django can send you the error messages. If the error occurs and django isn't handling it, it will generally show in your server's logs.
Personally, I think one of the best ways to view django errors is using raven with sentry, either self hosted or on getsentry. It even comes with a wsgi middleware to catch errors on the wsgi layer.
It looks like you get a timeout error, you can look at your webserver logs but it will probably show nothing interesting, I suppose you are doing something too heavy in your new middleware that takes too long and then times out the request.
I really suggest you to re-check your middleware code (maybe you can add the code here)