Pytigon's REST API capabilities are built on the fact that every generic view already serves dual purpose: the same URL that renders HTML can return JSON, XML, or CSV with a simple parameter change.
Add ?format=json to any Pytigon table view URL:
/tables_demo/table/Album/list/?format=json
Response:
{
"count": 25,
"offset": 0,
"rows": [
{
"id": 1,
"name": "Abbey Road",
"artist": "The Beatles",
"release_date": "1969-09-26",
"tracks_count": 17
},
...
]
}
The same URL without ?format=json returns the HTML table page. Same data, same URL, different representation.
| Format | URL Parameter | Content-Type |
|---|---|---|
| HTML | (default) | text/html |
| JSON | ?format=json or .../json/list/ |
application/json |
| CSV | .../csv/list/ or ?format=csv |
text/csv |
| XML | .../xml/list/ |
application/xml |
.../pdf/list/ |
application/pdf |
|
| XLSX | .../xlsx/list/ |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
For API-specific logic, define custom views using Django REST Framework or Pytigon's lightweight REST tools:
from pytigon_lib.schdjangoext.rest_tools import api_view, api_response
@api_view(['GET'])
def album_api(request):
albums = Album.objects.all()
data = [{"id": a.id, "name": a.name, "artist": a.artist} for a in albums]
return api_response(data)
The list view API supports standard query parameters:
/tables_demo/table/Album/list/?format=json&offset=0&sort=name&order=asc&search=Beatles
| Parameter | Purpose |
|---|---|
offset |
Pagination offset (0-based) |
sort |
Field to sort by |
order |
asc or desc |
search |
Full-text search across searchable fields |
Pytigon includes a REST client library for making API calls from server-side code or background tasks:
from pytigon_lib.schhttptools.rest_client import RestClient
client = RestClient('http://localhost:8000')
response = client.get('/tables_demo/table/Album/list/', params={'format': 'json'})
data = response.json()
REST endpoints support: - Django session authentication (for browser clients) - Token authentication (for API clients) - OAuth2 (for third-party integration) - JWT tokens (for mobile apps)
Configure authentication in settings_app.py as needed.