Easily integrate Open Forms in your Django application.
Project description
Open Forms Client (for Django)
- Version:
0.2.2
- Source:
https://github.com/open-formulieren/open-forms-client-django
- Keywords:
Open Forms, Client, Django
- PythonVersion:
3.7
About
Easily integrate Open Forms in your Django application. There are 3 main features:
Configuration to connect to Open Forms is added to your Django admin.
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.
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.
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:
Add open_forms_client to INSTALLED_APPS in your Django project’s settings.py:
INSTALLED_APPS = ( # ..., "openformsclient", )
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.
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>
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.
Done.
Gotcha’s
Open Forms configuration
Make sure the domain where you host your webapplication is in the Open Forms ALLOWED_HOSTS setting. Note that this is not the setting in your own webapplication but in the setting in the Open Forms installation.
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()),
Licence
Copyright © Maykin Media B.V., 2022
Licensed under the MIT.
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
Built Distribution
File details
Details for the file django-open-forms-client-0.2.2.tar.gz
.
File metadata
- Download URL: django-open-forms-client-0.2.2.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bc1563380f96aa23fe973458fec51c1097187fa6a539b70685aa4a6e5913433 |
|
MD5 | dc62d91479e9f2a7760b8317a9d64bb7 |
|
BLAKE2b-256 | 94ecd4c852738dea8b71d117bd6a0f7b0a0021a18376e75fc91ff380578f7085 |
File details
Details for the file django_open_forms_client-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: django_open_forms_client-0.2.2-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dddf75c582251a60c3d077b0a0b4e5f7845f7e8bae82289283ed249f0fefce0b |
|
MD5 | da5ee51dcef98759ab6f3feecc90c778 |
|
BLAKE2b-256 | 0d06d7fe90361312ca7a13e7521ab3c7ae540f851ac5e388c62d365c06d858ac |