Django component view for jinjax
Project description
dj_component_view
This project lets you create reusable Django views from jinjax templates.
Usage
Greeting.jinja
{#def name #}
<h1>hello, {{ name }}</h1>
views.py
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"),
}
index.html with htmx
<form hx-post="/greet" hx-trigger="submit">
<input type="text" name="name" placeholder="Enter your name" />
<button type="submit">Greet</button>
</form>
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.
class CustomView(ComponentView):
component = "CustomComponent"
method = "get"
...
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"
method = "get"
def get(self, request, *args, **kwargs):
# Custom implementation of the GET method
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for dj_component_view-3.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67ae5045276c6152764b85a64e4b4ba4b2162caf5ea64307daca26ea826d3f68 |
|
MD5 | dfa5d182e33312548c3659a20188ddce |
|
BLAKE2b-256 | 6acadbaa7777ec766f4c4f9a85b83228e28de002fa86bc1cc2c54488a4fa94de |