Skip to main content

Django CRUD utilities

Project description

Faster Django development

Get more out of less with a few design patterns:

  • Skip manual URL routing with MVC pattern and sane defaults
  • Dynamic navigation menus with tagged views
  • Skip get_context_data overrides by consuming the view object directly in templates
  • {% eval %} template tag to skip making template tags every time you want to call a function or method (like in Jinja2)
  • Secure by default: views allow only superusers by default, it's up to you to open permissions as-needed.

Install with:

pip install djmvc[bulma]

Routing

Skip manual URL routing definition by nesting views and controllers:

from djmvc.controller import Controller
from djmvc.view import View


# definition by sub-classing
class SubController(Controller):
    name = 'sub-controller'
    routes = [
        View.clone(
            name='sub-view',
        )
    ]


# or just define by cloning
Site = Controller.clone(
    name='controller',
    routes=[
        View.clone(
            name='view',
        ),
        SubController,
    ]
)

# define some importable url patterns to include
urlpatterns = Site().urlpatterns

# it will define reverseable urls
assert reverse('controller:view') == '/controller/view/'
assert reverse('controller:sub-controller:sub-view') == '/controller/sub-controller/sub-view/'

Templates

Ain't no way I'm defining get_context_data for everything I add to a view, I consider that adding a method or property to the view should make it directly accessible from the template, as such, the default TemplateMixin provides a get_context_data which already returns the view as context object.

from djmvc.views import generic

class MyView(generic.TemplateView):
    @property
    def something(self):
        return Some.thing()

Allows directly that in the template: {{ view.something }}.

Also, ain't no way I'm defining a templatetag for everything, loved the liberty Jinja2 gave me, taking it back with Django engine with the eval tag provided by djmvc.

class MyView(generic.TemplateView):
    def something(self, some_var, user):
        return Some.thing(some_var, user)

Becomes available as:

{% load eval %}
{% eval view.some_method "some test var" view.request.user as result %}
{{ result }}

Dynamic menus

Add any tags you like to your views:

class YourView(generic.TemplateView):
    tags = ['topbar']

    def has_permission(self):
        return self.request.user.is_authenticated  # the default

And use the get_tagged_view() method of the controller to get all views tagged "topbar" as such:

{% eval view.root_controller.get_tagged_views 'topbar' request=request as topbar_menu %}

Here, we're passing the request kwarg to get_tagged_views() which will instanciate the view object with passed kwargs and call has_permission() prior to returning the tagged view.

It also works with per-object permissions as such:

class YourModelDetailView(generic.DetailView):
    tags = ['object']

    def has_permission(self):
        return (
            self.request.user.is_superuser
            or self.object.owner == self.request.user
        )

Which will make that view show conditionnaly based on the request user, so that you can get all the views authorized for a user on a given object in the "object" tag as such:

{% eval view.root_controller.get_tagged_views 'object' request=request object=object as object_menu %}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

djmvc-0.0.1.tar.gz (28.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

djmvc-0.0.1-py3-none-any.whl (36.5 kB view details)

Uploaded Python 3

File details

Details for the file djmvc-0.0.1.tar.gz.

File metadata

  • Download URL: djmvc-0.0.1.tar.gz
  • Upload date:
  • Size: 28.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for djmvc-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b61ebf5f256bff84c8010117c7a297ac0aa99e1d3424232bb8893c528f1ab9d8
MD5 bee34f8dc491121a161b1ee1eb637e7a
BLAKE2b-256 92eb8ae63824f8bf867403d7939c5a5f3078195918e9a791d205da441e2f4385

See more details on using hashes here.

File details

Details for the file djmvc-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: djmvc-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 36.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for djmvc-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eff5d74f3ce14801cee77ccf345e81a1660c3abe17f8c57aa3a658ac68a9a0d3
MD5 e0a445f68194983b8ab0b88f34924782
BLAKE2b-256 ef9820da69fb216946bd9e7c3185786abfa3eff4b9146c0a903eb7178738df3f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page