iommi ===== iommi ===== .. raw:: html

Your first pick for a django power chord

.. image:: https://img.shields.io/badge/Code_on-GitHub-black :target: https://github.com/iommirocks/iommi .. image:: https://img.shields.io/discord/773470009795018763?logo=discord&logoColor=fff?label=Discord&color=7389d8 :target: https://discord.gg/ZyYRYhf7Pd .. image:: https://github.com/iommirocks/iommi/workflows/tests/badge.svg :target: https://github.com/iommirocks/iommi/actions?query=workflow%3Atests+branch%3Amaster .. image:: https://codecov.io/gh/iommirocks/iommi/branch/master/graph/badge.svg :target: https://codecov.io/gh/iommirocks/iommi .. image:: https://readthedocs.org/projects/iommi/badge/?version=latest :target: https://docs.iommi.rocks :alt: Documentation Status .. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/psf/black iommi is a toolkit to build web apps faster. It's built on Django but goes a lot further. It has: - :doc:`forms `: that feel familiar, but can handle growing complexity better than Django's forms - :doc:`tables `: that are powerful out of the box and scale up to arbitrary complexity - a system to :doc:`compose parts `:, like forms, menus, and tables, into bigger pages - tools that will speed up your development like live edit, jump to code, great feedback for missing select/prefetch related, a profiler, and more. - great error messages when you make a mistake .. image:: https://docs.iommi.rocks/README-demo.gif Example: .. code-block:: python class IndexPage(Page): title = html.h1('Supernaut') welcome_text = 'This is a discography of the best acts in music!' artists = Table(auto__model=Artist, page_size=5) albums = Table( auto__model=Album, page_size=5, ) tracks = Table(auto__model=Album, page_size=5) urlpatterns = [ path('', IndexPage().as_view()), ] This creates a page with three separate tables, a header and some text: .. image:: https://docs.iommi.rocks/README-screenshot.png For more examples, see the `examples project `_. Getting started --------------- See :doc:`getting started `. Running tests ------------- .. code-block:: make test make test-docs License ------- BSD :start-line: 4 Finding your way around ======================= These docs are split by what you need right now: - **Tutorials** are lessons to work through when you're new. Start here. - **How-to guides** answer "how do I ...?" when you have a job to do. - **Reference** is the dry description of every knob iommi has. - **Explanation** covers the thinking behind iommi, so your code comes out idiomatic. .. toctree:: :maxdepth: 2 :caption: Tutorials getting_started tutorial .. toctree:: :maxdepth: 2 :caption: How-to guides cookbook .. toctree:: :maxdepth: 2 :caption: Reference api common_config views dev_tools .. toctree:: :maxdepth: 2 :caption: Explanation components understanding .. toctree:: :maxdepth: 1 :caption: About the project imports history authors contributing @github @discord @pypi Indices and tables ================== * :ref:`genindex` * :ref:`search` .. _getting-started: Getting started =============== .. note:: This guide is intended for a reader that is well versed in the Django basics of the ORM, urls routing, function based views, and templates. 1. Install ---------- First: `pip install iommi`. Add `iommi` to installed apps: .. code-block:: python INSTALLED_APPS = [ # [...] 'iommi', ] Add iommi's middleware: .. code-block:: python MIDDLEWARE = [ # These three are optional, but highly recommended! 'iommi.live_edit.Middleware', # [... Django middleware ...] 'iommi.sql_trace.Middleware', 'iommi.profiling.Middleware', # [... your other middleware ...] 'iommi.middleware', ] .. note:: The iommi middleware must be the last middleware in the list! By default iommi uses a very basic bootstrap base template. We'll get to how to integrate it into your site later. 2. Your first form ------------------ From this point this guide is aimed at users trying iommi in an existing project. :doc:`The tutorial ` is a more in-depth guide to building a full application with iommi from scratch. Pick a model from your app, and let's build a create form for it! I'm using `Album` here, but you should replace it with your own model. Add this to your `urls.py`: .. code-block:: python from iommi import Form # Import any models you need from your models. Here I'm using Album from .models import Album urlpatterns = [ # ...your urls... path('iommi-form-test/', Form.create(auto__model=Album).as_view()), ] .. raw:: html
▼ Hide result
Toggle structure
3. Your first table ------------------- Pick a model from your app, and let's build a table for it! Add this to your `urls.py`: .. code-block:: python from iommi import Table # Import any models you need from your models. Here I'm using Album from .models import Album urlpatterns = [ # ...your urls... path('iommi-table-test/', Table(auto__model=Album).as_view()), ] .. raw:: html
▼ Hide result
Toggle structure
If you want, add a filter for some column: .. code-block:: python urlpatterns = [ # ...your urls... path('iommi-table-test/', Table( auto__model=Album, columns__name__filter__include=True, # <--- replace `name` with some field from your model ).as_view()), ] .. raw:: html
▼ Hide result
Toggle structure
4. Your first page ------------------ Pages are the method to compose complex pages from parts. Add this to your `views.py`: .. code-block:: python from iommi import Page, Form, Table # Import any models you need from your models. Here I'm using Artist from .models import Artist class TestPage(Page): create_form = Form.create(auto__model=Artist) a_table = Table(auto__model=Artist) class Meta: title = 'An iommi page!' then hook into `urls.py`: .. code-block:: python urlpatterns = [ # ...your urls... path( 'iommi-page-test/', TestPage().as_view() ), ] .. raw:: html
▼ Hide result
Toggle structure
5. A simple function based view ------------------------------- It's often useful to have a function based view around your iommi code to do some basic setup. So we'll add an example for that too. With iommi's middleware you can return iommi objects from your view: `views.py`: .. code-block:: python def iommi_view(request, name): return TestPage(title=f'Hello {name}') `urls.py`: .. code-block:: python urlpatterns = [ # ...your urls... path( 'iommi-view-test//', iommi_view ), ] .. raw:: html
▼ Hide result
Toggle structure
6. Make iommi pages fit into your projects design ------------------------------------------------- So far all the views we've created are rendered in plain bootstrap. Let's fit the iommi views you've already added into the design of your project. The simplest is to add something like this to your `settings.py`: .. code-block:: python # These imports need to be at the bottom of the file! from iommi import Style, Asset from iommi.style_bootstrap import bootstrap IOMMI_DEFAULT_STYLE = Style( bootstrap, base_template='my_project/iommi_base.html', root__assets=dict( my_project_custom_css=Asset.css(attrs__href='/static/custom.css'), my_project_custom_js=Asset.js(attrs__src='/static/custom.js'), ), ) Where `my_project/iommi_base.html` could look something like this: .. code-block:: html {% extends "iommi/base.html" %} {% block iommi_top %} {% include "my_menu.html" %} {% endblock %} {% block iommi_bottom %} {% include "my_footer.html" %} {% endblock %} After you've set up your base style successfully, all the test pages you made before (form, table, page, view) are now using your style. Tutorial ======== .. note:: This tutorial is intended for a reader that is well versed in the Django basics of the ORM, urls routing, function based views, and templates. It is also expected that you have already installed iommi in your project. Read section 1 of :ref:`Getting started `. In this tutorial you will build a discography app. By the end you will have: - an index page with album artwork - an artist page, and a page listing artists - an album page, and a page listing albums - a tracks page - the iommi admin, enabled for all of these Every step shows the code and the page it produces. Type the code in as you go; each step builds on the one before it. Set up ------ Put these models in your app's `models.py`: .. literalinclude:: models.py :pyobject: Genre :end-before: def __str__ .. literalinclude:: models.py :pyobject: Artist :end-before: def __str__ .. literalinclude:: models.py :pyobject: Album :end-before: def __str__ .. literalinclude:: models.py :pyobject: Track :end-before: def __str__ Create the tables: .. code-block:: shell python manage.py makemigrations python manage.py migrate Now load the same example data used in this tutorial, so your pages look like the screenshots. Download `big_discography.py`_ into your project and run it: .. code-block:: shell python manage.py shell < big_discography.py .. _big_discography.py: https://raw.githubusercontent.com/iommirocks/iommi/master/docs/custom/big_discography.py You're ready to build the first page. .. code-block:: python # Regenerates the example data script that the `Set up` section of this page # links to. Nothing is rendered into the docs from here. Tables ------ Creating a table view of a model in iommi is simple: .. code-block:: python urlpatterns = [ path('', Table(auto__model=Album).as_view()), ] .. raw:: html
▼ Hide result
Toggle structure
You get sorting and pagination by default, and we're using the default bootstrap 5 style. iommi ships with :ref:`more styles