Skip to main content

Django component view for jinjax

Project description

dj_component_view

This project lets you create reusable Django views from jinjax templates. By default, the .post method is implemented. You only need to override the .context method to pass data to the jinjax component.

Usage

from dj_component_view import ComponentView
from djecorator import Route

route = Route()

@route("/greet")
class GreetView(ComponentView):
    component = "Greeting"

    def context(self, request):
        return {
            # by default, the view expects a POST request
            "name": request.POST.get("name", "World"),
        }

Specifying the Allowed HTTP Method

You can set the method class variable in your ComponentView subclass to specify the allowed HTTP method for the view. The default value is None, which means both GET and POST methods are allowed.

  • If method is set to "GET", only GET requests will be allowed.
  • If method is set to "POST", only POST requests will be allowed.
  • If method is set to None (default), both GET and POST requests will be allowed.

If the incoming request's method does not match the specified method, a 405 Method Not Allowed response will be returned.

Overriding the get and post Methods

If you need more control over the handling of GET and POST requests, you can override the get and post methods in your ComponentView subclass.

@route("/custom")
class CustomView(ComponentView):
    component = "CustomComponent"

    def get(self, request, *args, **kwargs):
        # Custom implementation of the GET method
        ...

    def post(self, request, *args, **kwargs):
        # Custom implementation of the POST method
        ...

Providing Context Data

To provide context data to the rendered component, you can override the context method in your ComponentView subclass.

@route("/greet")
class GreetView(ComponentView):
    component = "Greeting"

    def context(self, request):
        return {
            "name": request.POST.get("name", "World"),
        }

The context method should return a dictionary containing the data that will be passed to the component for rendering.

with htmx

<form hx-post="/greet" hx-trigger="submit">
    <input type="text" name="name" placeholder="Enter your name">
    <button type="submit">Greet</button>
</form>

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

dj_component_view-2.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

dj_component_view-2.0-py2.py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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