dj_component_view
This project lets you create reusable Django views from jinjax templates.
Usage
Greeting.jinja
<h1>hello, {{ name }}</h1>
views.py
from djecorator import Route
from dj_component_view import component
route = Route()
@route("/components/greet")
@component("Greeting")
def greet(request):
return {
"name": request.GET.get("name", "World"),
}
index.html with htmx
<form hx-get="{{ url('greet') }}" hx-trigger="submit">
<input type="text" name="name" placeholder="Enter your name" />
<button type="submit">Greet</button>
</form>
There is also a lower-level API which the component decorator uses under the hood.
The ComponentView class inherits from django's own View class.
from dj_component_view import ComponentView
from djecorator import Route
route = Route()
@route("/greet", name="greet")
class GreetView(ComponentView):
component = "Greeting"
def render(self, request):
return {
"name": request.GET.get("name", "World"),
}
Specifying the Allowed HTTP Methods
You can set the methods class variable in your ComponentView subclass to specify the allowed HTTP methods for the view. The default value is ["GET"].
- If
methodsis set to["GET"], only GET requests will be allowed. - If
methodsis set to["POST"], only POST requests will be allowed. - If
methodsis set to["GET", "POST"], both GET and POST requests will be allowed.
@component("CustomComponent", methods=["post"])
def custom_component(request):
...
# or
class CustomView(ComponentView):
component = "CustomComponent"
methods = ["post"]
...
If the incoming request's method does not match any of the specified methods, 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"
methods = ["get"]
def get(self, request, *args, **kwargs):
# Custom implementation of the GET method
...
Metadata
Release files for dj_component_view 9.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dj_component_view-9.0.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dj_component_view-9.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 8.3 kB
Release files / dj_component_view-9.0.tar.gz
| Download URL | dj_component_view-9.0.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3b1d7606c03d7d45d1cb9c75bac223b043ef73f03cdc6a47787ca6c17c8c2b11
|
|
BLAKE2b-256 checksum How to use checksums |
daae357578ca8a39ac06d733ce6d0ba0f367650418d5131ae1d8d1138fa73c4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.1
|
Release files / dj_component_view-9.0-py2.py3-none-any.whl
| Download URL | dj_component_view-9.0-py2.py3-none-any.whl |
|---|---|
| Size | 3.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
9d10b188466fe8d11f3004b53717c200720198089b46d2249a860f8c794a4a03
|
|
BLAKE2b-256 checksum How to use checksums |
6628dc5519209277d4c076f4eda2ef9a52f94a4a938840190c69023d20b57e33
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.1
|