parts/django/docs/ref/contrib/databrowse.txt
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 ==========
       
     2 Databrowse
       
     3 ==========
       
     4 
       
     5 .. module:: django.contrib.databrowse
       
     6    :synopsis: Databrowse is a Django application that lets you browse your data.
       
     7 
       
     8 Databrowse is a Django application that lets you browse your data.
       
     9 
       
    10 As the Django admin dynamically creates an admin interface by introspecting
       
    11 your models, Databrowse dynamically creates a rich, browsable Web site by
       
    12 introspecting your models.
       
    13 
       
    14 .. admonition:: Note
       
    15 
       
    16     Databrowse is **very** new and is currently under active development. It
       
    17     may change substantially before the next Django release.
       
    18 
       
    19     With that said, it's easy to use, and it doesn't require writing any
       
    20     code. So you can play around with it today, with very little investment in
       
    21     time or coding.
       
    22 
       
    23 How to use Databrowse
       
    24 =====================
       
    25 
       
    26     1. Point Django at the default Databrowse templates. There are two ways to
       
    27        do this:
       
    28 
       
    29        * Add ``'django.contrib.databrowse'`` to your :setting:`INSTALLED_APPS`
       
    30          setting. This will work if your :setting:`TEMPLATE_LOADERS` setting
       
    31          includes the ``app_directories`` template loader (which is the case by
       
    32          default). See the :ref:`template loader docs <template-loaders>` for
       
    33          more.
       
    34 
       
    35        * Otherwise, determine the full filesystem path to the
       
    36          :file:`django/contrib/databrowse/templates` directory, and add that
       
    37          directory to your :setting:`TEMPLATE_DIRS` setting.
       
    38 
       
    39     2. Register a number of models with the Databrowse site::
       
    40 
       
    41            from django.contrib import databrowse
       
    42            from myapp.models import SomeModel, SomeOtherModel
       
    43 
       
    44            databrowse.site.register(SomeModel)
       
    45            databrowse.site.register(SomeOtherModel)
       
    46 
       
    47        Note that you should register the model *classes*, not instances.
       
    48 
       
    49        It doesn't matter where you put this, as long as it gets executed at some
       
    50        point. A good place for it is in your :doc:`URLconf file
       
    51        </topics/http/urls>` (``urls.py``).
       
    52 
       
    53     3. Change your URLconf to import the :mod:`~django.contrib.databrowse` module::
       
    54 
       
    55            from django.contrib import databrowse
       
    56 
       
    57        ...and add the following line to your URLconf::
       
    58 
       
    59            (r'^databrowse/(.*)', databrowse.site.root),
       
    60 
       
    61        The prefix doesn't matter -- you can use ``databrowse/`` or ``db/`` or
       
    62        whatever you'd like.
       
    63 
       
    64     4. Run the Django server and visit ``/databrowse/`` in your browser.
       
    65 
       
    66 Requiring user login
       
    67 ====================
       
    68 
       
    69 You can restrict access to logged-in users with only a few extra lines of
       
    70 code. Simply add the following import to your URLconf::
       
    71 
       
    72     from django.contrib.auth.decorators import login_required
       
    73 
       
    74 Then modify the :doc:`URLconf </topics/http/urls>` so that the
       
    75 :func:`databrowse.site.root` view is decorated with
       
    76 :func:`django.contrib.auth.decorators.login_required`::
       
    77 
       
    78     (r'^databrowse/(.*)', login_required(databrowse.site.root)),
       
    79 
       
    80 If you haven't already added support for user logins to your :doc:`URLconf
       
    81 </topics/http/urls>`, as described in the :doc:`user authentication docs
       
    82 </ref/contrib/auth>`, then you will need to do so now with the following
       
    83 mapping::
       
    84 
       
    85     (r'^accounts/login/$', 'django.contrib.auth.views.login'),
       
    86 
       
    87 The final step is to create the login form required by
       
    88 :func:`django.contrib.auth.views.login`. The
       
    89 :doc:`user authentication docs </ref/contrib/auth>` provide full details and a
       
    90 sample template that can be used for this purpose.