Django extensions for Hyperview mobile apps
Project description
django-hyperview
Django extensions for Hyperview mobile apps
Installation and Setup
Install with pip
python -m pip install django-hyperview
Configure
Add the app, once installed, to your INSTALLED_APPS
INSTALLED_APPS = [
...
"django_hv",
...
]
Optionally, you can add the HyperviewMiddleware
middleware to your settings.
MIDDLEWARE = [
...
"django_hv.middleware.HyperviewMiddleware",
]
Usage
xml-based form fields
As with Django, you can dynamically create your xml-based forms with this package.
from .models import Contact
from django_hv.forms import ModelForm
from django_hv import widgets
class ContactForm(ModelForm):
class Meta:
model = Contact
fields = ("first_name", "last_name", "phone_number", "email")
widgets = {
"first_name": widgets.TextField(attrs={"placeholder": "First name"}),
"last_name": widgets.TextField(attrs={"placeholder": "Last name"}),
"phone_number": widgets.TextField(attrs={"placeholder": "Phone number"}),
"email": widgets.TextField(attrs={"placeholder": "Email address"}),
}
In your view you can initiate the form as usual:
def contact_new(request):
context = {"form": ContactForm(request.POST or None)}
if request.method == "GET":
response = render(request, "new.xml", context)
elif request.method == "POST":
form = ContactForm(request.POST)
if form.is_valid():
context["saved"] = True
context["contact"] = form.save()
response = render(request, "form_fields.xml", context)
return hv_repond(response)
And the xml template, you can render the form as follows:
{% load hv_tags %}
<view xmlns="https://hyperview.org/hyperview" style="edit-group">
{% if saved %}
<behavior trigger="load" once="true" action="dispatch-event" event-name="contact-updated" />
<behavior trigger="load" once="true" action="reload" href="{{ contact.detail_url }}" />
{% endif %}
{% hv_csrf_token request %}
{{ form }}
</view>
Note that you can create a CSRF token if you load the template tags of this package: {% hv_csrf_token request %}
. It needs the request
as an argument since Django requires that object to create a token.
Hyperview response
You can use this package for your views.
from .models import Contact
from .forms import ContactForm
from django_hv.http import hv_repond
def contact_detail(request, id):
response = render(request, "show.xml", {"contact": Contact.objects.get(id=id)})
return hv_repond(response)
def contact_new(request):
...
return HyperviewResponse(request, "new.xml", context={})
Check if the request comes from a Hypermedia client
For this to work, you need to add HyperviewMiddleware
to MIDDLEWARE
.
def contact_detail(request, id):
template = "show.xml" if request.hv else "show.html"
response = render(request, template, {"contact": Contact.objects.get(id=id)})
return hv_repond(response)
The property request.hv
checks if the request has a specific header that is passed by the Hypermedia client.
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
Hashes for django_hyperview-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68fbbe761ab2fc950b9de4c8e2be82dbe7b7467eab5b0e7ab950a99772079c59 |
|
MD5 | c8c05ec0cbd66a7f9fdb1aaaaff4dfc3 |
|
BLAKE2b-256 | 5a4ae68aea6ef23636425898b0e8163ecf81da302aba45bc8b352ffe272752e9 |