No, I happen to agree with your approach. As you point out, in principle it’s similar to how the admin works. However, the admin goes a bit further in that it’s dynamically creating the form by introspection of the models (along with the information being provided by the Admin class).
Lookin at the source of django.contrib.admin.templates :: its chaotic 95% tags, that’s probably impossible to reuse the views equally complex
my latest attempt
test.html
<form action="{% url 'test' %}?id={{formId}}" method="POST" name="form">
{% csrf_token %}
{{ form }}
<button class="btn btn-outline-primary my-3" type="submit">Save</button>
</form>
view.py
def test_view(request):
id = request.GET.get('id')
if id == 'city':
form = CityForm(request.POST or None)
if id == 'region':
form = RegionForm(request.POST or None)
if id == 'branch':
form = BranchForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
form.save()
# or return redirect('index')
return redirect('/test/test/?id=city')
return render(request, 'test/test.html', {"form": form, 'formId': id})
This method however reload the page, how could I load the form (almost like iframe) in to
That’s not something under the control of Django.
If the browser is making a page request, it’s going to treat the response as a new page.
If you have JavaScript making the call, then the JavaScript is responsible for handling the response. (It could be a new page, or it could be an HTML fragment to inject into the DOM, or it could be JSON that is decoded to update the DOM.)
I guess need to do more research and run a few examples on fetch method to wait for response. That subject is for a different forum