您可以使用django-jsonforms https://pypi.org/project/django-jsonforms/像这样:
形式.py:
from django.forms import ModelForm, Form
from django_jsonforms.forms import JSONSchemaField
first_name_schema = {
"type": "object",
"required": ["First Name"],
"properties": {
"First Name": {
"type": "string",
"maxLength": 30
}
}
}
last_name_schema = {
"type": "object",
"required": ["Last Name"],
"properties": {
"Last Name": {
"type": "string",
"maxLength": 30,
}
}
}
options = {"no_additional_properties": True}
class CustomForm(Form):
first_name = JSONSchemaField(schema = first_name_schema, options = options)
last_name = JSONSchemaField(schema = last_name_schema, options = options)
视图.py:
from .forms import CustomForm
def some_view(request):
return render(request, 'some_html.html', {'form': CustomForm()})
一些_html:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="some_form" action="/path/to/some_view/" method="post">
{% csrf_token %}
{{ form.media }}
{{ form }}
<button type="submit">submit</button>
</form>
欲了解更多信息,请访问docs https://github.com/json-editor/json-editor
Edit:
您可以通过修改选项字典来删除表单中的额外按钮,如下所示:
options = {
"no_additional_properties": True,
"disable_collapse": True,
"disable_edit_json": True,
"disable_properties": True
}