Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘django’

logging in Django

Logging to console

1. Use Print

e.g.

def index(req):

    print “hello!”

and you will see the ‘hello!’ on your console. (or apache log if running on linux)

2. Use Python logging feature

# in settings.py

import logging

logging.basicConfig(

    level = logging.DEBUG,

    format = ‘%(asctime)s %(levelname)s %(message)s’

)

# write log

import logging

logging.debug(‘hello’)

and you can also see the ‘hello’ on your console.

 

Logging to a file

# in settings.py

import logging

logging.basicConfig(

    level = logging.DEBUG,

    format = ‘%(asctime)s %(levelname)s %(message)s’,

    filename = ‘/tmp/django.log’,

    filemode=’w’

)

# write log

import logging

logging.debug(‘hello’)

you can see your ‘hello’ in the ‘/tmp’/django.log’ file

Tips for working with template inheritance in Django

  1. If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won’t work, otherwise.
  2. More {% block %} tags in your base templates are better. Remember, child templates don’t have to define all parent blocks, so you can fill in reasonable defaults in a number of blocks, then only define the ones you need later. It’s better to have more hooks than fewer hooks.
  3. If you find yourself duplicating content in a number of templates, it probably means you should move that content to a {% block %} in a parent template.
  4. If you need to get the content of the block from the parent template, the {{ block.super }} variable will do the trick. This is useful if you want to add to the contents of a parent block instead of completely overriding it. Data inserted using {{ block.super }} will not be automatically escaped (see the next section), since
    it was already escaped, if necessary, in the parent template.
  5. For extra readability, you can optionally give a name to your {% endblock %} tag. For example:
    {% block content %}

    {% endblock content %}
    In larger templates, this technique helps you see which {% block %} tags are being closed.