Books
Arun Ravindran

Django Design Patterns and Best Practices

  • Николай Сайкоhas quoted8 years ago
    quick and easy profiling needs, django-debug-toolbar is quite handy. We can also use Python profiling tools, such as the hotshot module for detailed analysis
  • Николай Сайкоhas quoted8 years ago
    A good starting point for frontend optimization would be to check your site with Google Page Speed or Yahoo! YSlow (commonly used as browser plugins). These tools will rate your site and recommend various best practices, such as minimizing the number of HTTP requests or gzipping the content.
  • Николай Сайкоhas quoted8 years ago
    There are several Django packages that offer static asset management such as django-pipeline or webassets.
  • Николай Сайкоhas quoted8 years ago
    Lord Kelvin would say:
    If you can't measure it, you can't improve it.
  • Николай Сайкоhas quoted8 years ago
    Monitoring tools usually need a backend service (sometimes called agents) to collect the statistics, and a frontend service to display dashboards or generate reports. Popular data collection backends include StatsD and Monit. This data can be passed to frontend tools, such as Graphite.
    There are several hosted monitoring tools, such as New Relic and Status.io, which are easier to set up and use.
  • Николай Сайкоhas quoted8 years ago
    you might want to pause in the middle of a template to inspect the state (say, inside a loop). A debugger would be perfect for such cases. In fact, it is possible to use any one of the aforementioned Python debuggers for your templates using custom template tags.
    Here is a simple implementation of such a template tag. Create the following file inside a templatetag package directory:
    # templatetags/debug.py
    import pudb as dbg # Change to any *db
    from django.template import Library, Node
    register = Library()
    class PdbNode(Node):
    def render(self, context):
    dbg.set_trace() # Debugger will stop here
    return ''
    @register.tag
    def pdb(parser, token):
    return PdbNode()
    In your template, load the template tag library, insert the pdb tag wherever you need the execution to pause, and enter the debugger:
    {% load debug %}
    {% for item in items %}
    {# Some place you want to break #}
    {% pdb %}
    {% endfor %}
    Within the debugger, you can examine anything, including the context variables using the context dictionary:
    >>> print(context["item"])
    Item0
    If you need more such template tags for debugging and introspection, then I would recommend that you check out the django-template-debug package
  • Николай Сайкоhas quoted8 years ago
    PuDB is my preferred replacement for pdb. It is so intuitive that even beginners can easily use this interface. Like pdb, just insert the following code to break the execution of the program:
    import pudb; pudb.set_trace()
  • Николай Сайкоhas quoted8 years ago
    you have configured the LOGGING variable in settings.py, adding a logger to your existing code is quite easy, as shown here:
    # views.py
    import logging
    logger = logging.getLogger(__name__)

    def complicated_view():
    logger.debug("Entered the complicated_view()!")
  • Николай Сайкоhas quoted8 years ago
    You can modernize a legacy application written in other languages or frameworks by importing their database structure into Django. As an immediate advantage, you can use the Django admin interface to view and change your legacy data.
    Django makes this easy with the inspectdb management command, which looks as follows:
    $ python manage.py inspectdb > models.py
    This command, if run while your settings are configured to use the legacy database, can automatically generate the Python code that would go into your models file.
  • Николай Сайкоhas quoted8 years ago
    On Ubuntu, you will need the following packages installed to install PyGraphviz:
    $ sudo apt-get install python3.4-dev graphviz libgraphviz-dev pkg-config
    Now activate your virtual environment and run pip to install the development version of PyGraphviz directly from GitHub, which supports Python 3:
    $ pip install git+http://github.com/pygraphviz/pygraphviz.git#egg=pygraphviz
    Next, install django-extensions and add it to your INSTALLED_APPS. Now, you are all set.
    Here is a sample usage to create a GraphViz dot file for just two apps and to convert it to a PNG image for viewing:
    $ python manage.py graph_models app1 app2 > models.dot
    $ dot -Tpng models.dot -o models.png
fb2epub
Drag & drop your files (not more than 5 at once)