Skip to main content

Open Forms Client (for Django)

Version:

0.4.0

Source:

https://github.com/open-formulieren/open-forms-client-django

Keywords:

Open Forms, Client, Django

PythonVersion:

3.7

Build status Code quality checks black Coverage status

python-versions django-versions pypi-version

About

Easily integrate Open Forms in your Django application. There are 3 main features:

  1. Configuration to connect to Open Forms is added to your Django admin.

  2. By adding an OpenFormsField in any Django model, you get a list of forms in Open Forms to choose from in the Django admin or other Django forms.

  3. You get templatetags to render an Open Forms form in your webpage.

If you have Sentry installed and you enable Sentry in the Django admin configuration page, it will use your existing configuration to connect to Sentry.

Ordered dashboard with dropdown menu. Ordered dashboard with dropdown menu. Ordered dashboard with dropdown menu.

Installation

Requirements

  • Python 3.7 or newer

  • Django 3.2 or newer

Install

You can install Open Forms Client either via the Python Package Index (PyPI) or from source.

To install using pip:

pip install django-open-forms-client

Usage

To use this with your project you need to follow these steps:

  1. Add open_forms_client to INSTALLED_APPS in your Django project’s settings.py:

    INSTALLED_APPS = (
        # ...,
        "openformsclient",
    )
  2. Add an OpenFormsField to your relevant models (like a Page model):

    from openformsclient.models import OpenFormsField
    
    class Page(models.Model):
        # ...
        form = OpenFormsSlugField(blank=True)

    There is also a OpenFormsUUIDField that stores the UUID of the form instead of the “slug”. This is more precise but if someone replaces a form in Open Forms, the UUID will change but the slug might remain the same.

  3. Add the templatetags {% openforms_sdk_media %} and {% openforms_form page.form %} to your templates, to render an Open Forms form:

    {% load openforms %}
    <!-- Optional to render Open Forms in the proper language -->
    <html lang="nl">
    <head>
        <!-- Required for icons used by Open Forms -->
        <meta charset="utf-8">
    
        {% openforms_sdk_media %}
    </head>
    <body>
    
    {% if page.form %}
        {% openforms_form page.form %}
    {% else %}
        <p>This page has no form</p>
    {% endif %}
    
    </body>
    </html>
  4. Configure your Open Forms connection and settings in the admin, under Open Forms client configuration. Once the status field shows a green icon, your configuration is working.

  5. Done.

Templatetags

There are 4 templatetags available with several parameters. All parameters translate to Open Forms SDK parameters.

{% load openforms %}
{% openforms_form form_id csp_nonce base_path lang html_id %}
{% openforms_sdk_media %}
{% openforms_sdk_js %}
{% openforms_sdk_css %}

Gotcha’s

Open Forms configuration

Note that these are not settings in your own webapplication but they should be set correctly in the Open Forms installation.

  • ALLOWED_HOSTS contains your domain name.

  • CSRF_TRUSTED_ORIGINS contains your domain name.

  • CSRF_COOKIE_SAMESITE should be "none".

CSP headers

When your webapplication uses CSP headers you need to pass the csp_nonce to the openforms_form templatetag as well. If you use Django-CSP you can do this:

{% load openforms %}
{% openforms_form page.form csp_nonce=request.csp_nonce %}

Additionally, you need to allow your webapplication to load styles and scripts from the Open Forms SDK and connect to the Open Forms API. When using Django-CSP some options need to be changed in your settings.py:

# The Open Forms SDK files might differ from the API domain. Note that this
# the same domain as configured in the Open Forms configuration model. You
# might do something smart to use that value here.
OPEN_FORMS_API_DOMAIN = "forms.example.com"
OPEN_FORMS_SDK_DOMAIN = OPEN_FORMS_API_DOMAIN

# Allow your webapp to load styles from Open Forms SDK.
CSP_STYLE_SRC = ("'self'", OPEN_FORMS_SDK_DOMAIN)

# Allow your webapp to load script from Open Forms SDK.
CSP_SCRIPT_SRC = ("'self'", OPEN_FORMS_SDK_DOMAIN)

# Allow your webapp to load images from Open Forms SDK.
CSP_IMG_SRC = ("'self'", OPEN_FORMS_SDK_DOMAIN)

# Allow your webapp to load fonts from Open Forms SDK.
CSP_FONT_SRC = ("'self'", OPEN_FORMS_SDK_DOMAIN)

# Allow your webapp to connect to the Open Forms API.
CSP_CONNECT_SRC = ("'self'", OPEN_FORMS_API_DOMAIN)

Make page refreshes work

The URL changes when you start a form, indicating the step you are currently on. Refreshing the page will result in a HTTP 404 because this URL does not actually exist. You need to catch these URL-patterns and redirect the user back to the form. You can so like this:

# urls.py

# The view thats starts the form
path("page/<slug:slug>", PageView.as_view(), name="page"),
# Whenever you refresh the page that has the form, the URL might be changed
# and needs to redirect the user to the start of the form.
path("page/<slug:slug>/<path:rest>", PageView.as_view()),

Form shows a CSRF error

This can have many reasons because by default, you typically don’t want cross-site requests. The whole point of this client however, is to allow cross-site requests from your website to Open Forms.

Make sure your (not Open Forms) SECURE_REFERER_POLICY Django setting is set to origin-when-cross-origin or less strict. In Django 3.1 this was made more strict by default.

If this is set correctly and you still get this error, see above settings if your Open Forms installation was correctly configured.

Form won’t start

If you can see the form startpage but when you click “start” it doesn’t do anything (or you see a CSRF error in your browser log), you are most likely logged in to Open Forms as admin user. Log out of Open Forms or use incognito mode to start the form.

This is a known issue.

Licence

Copyright © Maykin Media B.V., 2022

Licensed under the MIT.

Metadata

Release files for django-open-forms-client 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-open-forms-client 0.4.0
File Size Uploaded Provenance
django_open_forms_client-0.4.0.tar.gz 14.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for django-open-forms-client 0.4.0
File Interpreter ABI Platform
django_open_forms_client-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.6 kB

Release files / django_open_forms_client-0.4.0.tar.gz

Download URL django_open_forms_client-0.4.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8319b3720ff39d6e043598ddec58d88122e797f208e730730ff221c2ab14ed79
BLAKE2b-256 checksum
How to use checksums
d02a121a69e46e0f2a709ae4cae7086018e85024b9cdbd756611c24a7fd3ef0a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Release files / django_open_forms_client-0.4.0-py3-none-any.whl

Download URL django_open_forms_client-0.4.0-py3-none-any.whl
Size 14.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bc5762c97b316ec48462882358b251478acb94e817f046f0d44265030b638013
BLAKE2b-256 checksum
How to use checksums
6d748cb5b97606907804167422e3890e1967e709785e2539853cc883725208c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.9.19

Security for django-open-forms-client 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page