Package: django-open-forms-client
Open Forms Client (for Django)Version:0.3.0Source:https://github.com/open-formulieren/open-forms-client-djangoKeywords:Open Forms, Client, DjangoPythonVersion:3.7AboutEasily integrateOpen Formsin your Django application. There are 3 main
features:Configuration to connect to Open Forms is added to your Django admin.By adding anOpenFormsFieldin 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 haveSentryinstalled and you enable Sentry in the Django admin
configuration page, it will use your existing configuration to connect to
Sentry.InstallationRequirementsPython 3.7 or newerDjango 3.2 or newerInstallYou can install Open Forms Client either via the Python Package Index (PyPI) or
from source.To install usingpip:pipinstalldjango-open-forms-clientUsageTo use this with your project you need to follow these steps:Addopen_forms_clienttoINSTALLED_APPSin your Django project’ssettings.py:INSTALLED_APPS=(# ...,"openformsclient",)Add anOpenFormsFieldto your relevant models (like aPagemodel):fromopenformsclient.modelsimportOpenFormsFieldclassPage(models.Model):# ...form=OpenFormsSlugField(blank=True)There is also aOpenFormsUUIDFieldthat 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:{%loadopenforms%}<!-- 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>{%ifpage.form%}{%openforms_formpage.form%}{%else%}<p>This page has no form</p>{%endif%}</body>
</html>Configure your Open Forms connection and settings in the admin, underOpen Forms client configuration. Once thestatusfield shows a green
icon, your configuration is working.Done.TemplatetagsThere are 4 templatetags available with several parameters. All parameters
translate toOpen Forms SDKparameters.{%loadopenforms%}{%openforms_formform_idcsp_noncebase_pathlanghtml_id%}{%openforms_sdk_media%}{%openforms_sdk_js%}{%openforms_sdk_css%}Gotcha’sOpen Forms configurationNote that these arenotsettings in your own webapplication but they should
be set correctly in the Open Forms installation.ALLOWED_HOSTScontains your domain name.CSRF_TRUSTED_ORIGINScontains your domain name.CSRF_COOKIE_SAMESITEshould be"none".CSP headersWhen your webapplication usesCSP headersyou need to pass thecsp_nonceto theopenforms_formtemplatetag as well. If you useDjango-CSPyou 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 usingDjango-CSPsome options need to be changed in yoursettings.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 workThe 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 formpath("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 errorThis 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_POLICYDjango setting is set
toorigin-when-cross-originor 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 startIf 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 aknown issue.LicenceCopyright ©Maykin Media B.V., 2022Licensed under theMIT.