Django development console custom/debug messages - django

Is there a way to print messages to Django development console?
I'm running 1.1 Django in buildout environment.
So running bin/django runserver (manage.py runserver) it shows development console where it writes resources, actions taken (equivalent of apache access/error logs(?)).
I want to print custom messages to that console for debugging purposes that it wouldn't kill the action just notice me.
Should i use logging module or is there other way?

If you're sure you just want it in development, you can simply use print - the message appears in the console.
However, you will need to delete the print statement before you deploy to production, as this will cause an error with Apache.
A better long-term solution is to use the logging module, configured to output to the console.

Related

Catching Server Error 500 using logging in Django (Server gunicorn)

I have an application deployed on Heroku and sometimes it throws the server error 500 randomly. I am trying to implement logging in my project. I am reading the docs but I am missing some critical piece of information. The docs say, Once you have configured your loggers, handlers, filters and formatters, you need to place logging calls into your code. My question is where exactly to put the logging code because I have already used try-except for all the parts, as far as I know, that may throw an error?
I have done a tutorial on the topic and reading the docs but that doesn't seem to convey the message to me.
I also sometimes deploy to Heroku, and like the combination of Papertrail and Sentry.
Papertrail I use for logging, including print statements to the console.
Sentry I setup for the error handling, such as what you mention random 500 errors. The plugin for Django automatically connects the 500 errors you see under debug mode and sends that information to their cloud for further processing
For Heroku both are readily available on the marketplace

Apache/wsgi/django configuration

We have two django applications running on the same server that interact with an API that uses oauth. They function as expected, communicating with each other, when run under the django development server. However, when deployed using apache/wsgi they don't work together.
(To be more specific, one application is an instance of the Indivo server; the other one is a custom application that interacts with Indivo.)
What is the best way to trouble shoot this?
Make sure that the Django instances are working by themselves first. For example, one app could be started under Apache, and the other using ./manage.py runserver. Reverse which one is running using Apache and verify that all works as expected.
Use the Apache error logs to look for errors such as failed requests.
Since one of your apps appears to implement a web API, use something like the Google Chrome Postman App to exercise the site from a web browser.
Learn how to use the Django logging framework to log information about your apps as they execute.

How can I use Django to write a console app (i.e., text-based & used from a terminal) that runs over the Internet?

There is so much power at the command line. Web pages are for non-power-users. I'd like my Django app to also have a console-based interface so that people can really work fast and not fiddle with graphics and a mouse. Has anyone tried doing something like this using Django? I like the example of heroku -- once you create an account at heroku.com you can do so much at the command line and interact with the server without the overhead of HTTP.
This is exactly what the 'shell' command of manage.py does. It gives you a python prompt with a few things set up so you can import your models and mess with them via the django API. So if your command-line tools are for superusers with shell access on the server then you can just write some python scripts that get run in the same way.
However, if you want access to users from other machines then you (obviously?) need to go via HTTP, but python has libraries for doing http requests. The only complication is with logins and cookies, but python's libraries can help you with that too - see urllib2, cookielib etc. You'd have to write some plain-text templates for the returned output.
I have no experience with heroku so I'm not sure what it's doing - what kind of thing can you do at the command line with it?

Django application deployment problem and error logs

I am trying to deploy my django application to VPS but I get 505- Internal Server Error and it is normal for the first time.
However, the admin of the VPS server do not allow me to see the error logs of the Apache Server. In such situation, is there any way to view the real error messagges/logs ?
Thanks
If you deployed your app with DEBUG=True maybe the error message can help you.
Your vps admin should give to you at least a copy of your log files.. i don't see another way..

Profiling django app hosted on apache2

I am having a django app which was hosted on apache2. The webapp basically makes a request to a server using thrift and renders the output on the webpage. I notice that webapp is really slow. I am not sure if it is machine problem or the API problem. I verified the API's and they are returning responses in few milliseconds. I am not sure, if django is the problem. Is there a way to profile the webapp. I am using python 2.5.2.
Please help.
Thank you
the simplest thing would be to enable logging, if you are using the latest django 1.3, it is nicely integrated with the python logging module, see:
http://docs.djangoproject.com/en/1.3/topics/logging/
here you can define a Formatter which saves the time each log message is written, see example in django docs:
http://docs.djangoproject.com/en/1.3/topics/logging/#an-example