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