neapolitan

repository·main·Indexed 20 days ago

https://github.com/carltongibson/neapolitan

A Django library designed to quickly generate standard CRUD (Create, Read, Update, Delete) views for Django models with minimal boilerplate. It provides the CRUDView class for automated operations, built-in TailwindCSS templates, and a management command (mktemplate) to bootstrap custom templates. Neapolitan includes reusable template tags and flexible methods for customizing QuerySets, form handling, pagination, and filtering.

Tokens
4.8K
Snippets
19
Records
34
Agent score
71%

What's inside neapolitan

  1. Overview of Neapolitan

    main
    Neapolitan is a reusable library for Django projects designed to provide quick CRUD views for applications. It aims to provide frontend CRUD access similar to Django's admin functionality, including base templates and reusable template tags, to reduce the boilerplate required to implement frontend management interfaces.
  2. Understand Neapolitan versioning

    main
    Neapolitan uses a two-part CalVer versioning scheme (e.g., 23.7), where the first number represents the year and the second represents the release number within that year. The project aims to support current Django and Python versions until they reach end-of-life.
  3. Use CRUDView for automated CRUD operations

    main
    The CRUDView class in neapolitan.views is the primary abstraction for building standard CRUD (Create, Read, Update, Delete) interfaces for a Django model. It provides built-in request handlers for list, detail, create, edit, and delete views, reducing the boilerplate required to implement these standard patterns.
  4. Set up the base template for Neapolitan

    main

    Neapolitan's built-in templates expect to extend a base template named base.html. You must provide this file in your project's template directory (e.g., dashboard/templates/base.html) and include a content block.

    Neapolitan uses TailwindCSS for styling. For quick prototyping, you can include the Tailwind Play CDN in your base.html, though this is not recommended for production.

    <!doctype html>
    <html>
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <script src="https://cdn.tailwindcss.com"></script>
        </head>
        <body>
            {% block content %}{% endblock %}
        </body>
    </html>
  5. Create CRUD views for Django models with CRUDView

    main

    Neapolitan provides a CRUDView class to quickly generate full CRUD (Create, Read, Update, Delete) functionality for your Django models. To use it, subclass neapolitan.views.CRUDView, specify the model you want to manage, and define the fields that should be exposed in the views.

    To register the views in your project, use the get_urls() method on your custom view class and include its output in your urlpatterns list.

    # urls.py
    from neapolitan.views import CRUDView
    from .models import Bookmark
    
    class BookmarkView(CRUDView):
        model = Bookmark
        fields = ["url", "title", "note"]
    
    urlpatterns = [ ... ] + BookmarkView.get_urls()
  6. Override Neapolitan templates using mktemplate

    main

    Neapolitan provides default templates that use TailwindCSS for styling. You can override these templates for specific models by using the mktemplate management command. This copies the active Neapolitan template to your app's templates directory with a name specific to your model, allowing CRUDView to pick it up.

    To list available templates for a model, use:

    python manage.py mktemplate myapp.MyModel --list

    To copy a template for a specific model, provide the model in app_name.ModelName format. The command will place the template in templates/myapp/mymodel_list.html (for example) so it can be used by the corresponding CRUDView.

  7. Implement project-level overrides for CRUDView

    main

    If the default CRUDView behavior does not meet your requirements, you can implement project-level overrides by creating a custom base class in your views.py.

    To do this, import CRUDView from neapolitan.views as a base class (e.g., BaseCRUDView), define your own CRUDView class inheriting from it, and place your overrides within that class. All subsequent model-specific views should then inherit from your custom CRUDView instead of the Neapolitan original. This pattern allows you to easily revert to the standard Neapolitan implementation by simply removing your custom base class and updating the imports.

    from neapolitan.views import CRUDView as BaseCRUDView
    
    
    class CRUDView(BaseCRUDView):
        # Add your overrides here.
    
    
    class MyModelCRUDView(CRUDView):
        model = "MyModel"
        fields = ["name", "description"]
  8. Use CRUDView to generate model views

    main

    The CRUDView is the core of Neapolitan. It provides standard list, detail, create, edit, and delete views for a Django model. You instantiate a specific view by calling .as_view(role=Role.X) where X is one of the available roles. The role determines which HTTP methods (GET, POST, etc.) are mapped to which internal handlers (e.g., list, show_form, process_form).

    from neapolitan.views import CRUDView, Role
    
    class MyModelView(CRUDView):
        model = MyModel
    
    # To use as a list view:
    urlpatterns = [
        path('items/', MyModelView.as_view(role=Role.LIST)),
    ]