Stage 0 — Register a model

Goal

Add a Django model and expose default CRUD routes with a ModelController in djmvc.py.

Model

from django.db import models


class Item(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

Controller and registration

Create yourapp/djmvc.py, add yourapp to INSTALLED_APPS, define your controller, and append it to djmvc.site:

import djmvc

from .models import Item


class ItemController(djmvc.ModelController):
    model = Item
    icon = 'inbox'
    routes = djmvc.ModelController.routes + [
        djmvc.generic.ListView.clone(site_search=True),
    ]


djmvc.site.routes.append(ItemController)

Before build(), site.routes is the declaration list; append() adds your controller there. Build then autodiscovers every djmvc.py module (importing it runs the append) and gives you list, create, detail, update, delete, and bulk-delete views with Bulma templates.

After migrate, log in (see Install djmvc) and visit http://localhost:8000/item/. URL names look like site:item:list, site:item:create.

Default item list with sidebar navigation

Default list view for the Item model — table, sidebar navigation, and create action.

List and detail use Django’s view permission (view_item). Create, update, and delete use add, change, and delete.

Tests

tests/test_stage0.py on GitHub