neapolitan
repository·main·Indexed 20 days ago
https://github.com/carltongibson/neapolitanA 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.
What's inside neapolitan
- 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.
Understand Neapolitan versioning
mainNeapolitan 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.Use CRUDView for automated CRUD operations
mainTheCRUDViewclass inneapolitan.viewsis 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.Template requirements for Neapolitan
mainNeapolitan provides base templates and reusable template tags. For these to work, your project must have abase.htmltemplate that defines a{% block content %}block.Install Django and Neapolitan
mainTo get started with Neapolitan, install both
djangoandneapolitaninto your Python environment using pip.pip install django neapolitanSet up the base template for Neapolitan
mainNeapolitan'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 acontentblock.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>Configure Neapolitan in Django settings
mainAfter creating your Django application (e.g.,
projects), you must add both your application andneapolitanto theINSTALLED_APPSlist in yoursettings.pyfile.INSTALLED_APPS = [ 'projects', 'neapolitan', # ... ]Create CRUD views for Django models with CRUDView
mainNeapolitan provides a
CRUDViewclass to quickly generate full CRUD (Create, Read, Update, Delete) functionality for your Django models. To use it, subclassneapolitan.views.CRUDView, specify themodelyou want to manage, and define thefieldsthat 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 yoururlpatternslist.# urls.py from neapolitan.views import CRUDView from .models import Bookmark class BookmarkView(CRUDView): model = Bookmark fields = ["url", "title", "note"] urlpatterns = [ ... ] + BookmarkView.get_urls()Override Neapolitan templates using mktemplate
mainNeapolitan provides default templates that use TailwindCSS for styling. You can override these templates for specific models by using the
mktemplatemanagement command. This copies the active Neapolitan template to your app'stemplatesdirectory with a name specific to your model, allowingCRUDViewto pick it up.To list available templates for a model, use:
python manage.py mktemplate myapp.MyModel --listTo copy a template for a specific model, provide the model in
app_name.ModelNameformat. The command will place the template intemplates/myapp/mymodel_list.html(for example) so it can be used by the correspondingCRUDView.Implement project-level overrides for CRUDView
mainIf the default
CRUDViewbehavior does not meet your requirements, you can implement project-level overrides by creating a custom base class in yourviews.py.To do this, import
CRUDViewfromneapolitan.viewsas a base class (e.g.,BaseCRUDView), define your ownCRUDViewclass inheriting from it, and place your overrides within that class. All subsequent model-specific views should then inherit from your customCRUDViewinstead 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"]Install Neapolitan via pip
mainInstall the Neapolitan package using pip:
pip install neapolitanUse CRUDView to generate model views
mainThe
CRUDViewis 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)whereXis one of the available roles. Theroledetermines 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)), ]