Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-hyperview-0.0.2.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

django_hyperview-0.0.2-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file django-hyperview-0.0.2.tar.gz.

File metadata

  • Download URL: django-hyperview-0.0.2.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/42.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for django-hyperview-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f88c9003855b4f8093ed38e6a6e161e63d27d477ede26addcb6a9ad7a771432e
MD5 7ee41ef137bcfb0e6a63cbbb4aa29b0a
BLAKE2b-256 431bc6abb94f86fd374ea345de7df90d2ba8227982064bd4054e1faa0c6d977e

See more details on using hashes here.

File details

Details for the file django_hyperview-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_hyperview-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/42.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12

File hashes

Hashes for django_hyperview-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 68fbbe761ab2fc950b9de4c8e2be82dbe7b7467eab5b0e7ab950a99772079c59
MD5 c8c05ec0cbd66a7f9fdb1aaaaff4dfc3
BLAKE2b-256 5a4ae68aea6ef23636425898b0e8163ecf81da302aba45bc8b352ffe272752e9

See more details on using hashes here.

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