如何在HTML temp中调用窗体视图

2024-05-14 06:33:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想制作一个HTML模板,其中我想制作三个表单(A、B和C)。 A窗体应作为默认值加载,并有两个按钮打开窗体B和C。窗体应调用视图。A并获取数据。你知道吗

现在点击按钮,表单B应该加载并从视图中获取数据。BC相同

<form name='A' > 
    //should call view.A 
    button B <-- should call Form B and form B grab data from B 
    button C <-- should call Form c and form C grab data from C 
</form>

我不知道这是否可能。 请帮忙。你知道吗


Tags: andfromform视图模板表单datahtml
1条回答
网友
1楼 · 发布于 2024-05-14 06:33:54

从技术上讲,您可以使用一个视图和一个模板来呈现3个表单,然后有一个操作url引用3个单独的视图,如下所示:

def 3_forms_view(request):
    form_a = YourFormA(request.POST or None)
    form_b = YourFormB(request.POST or None)
    form_c = YourFormC(request.POST or None)
    context = {
        "form_a": form_a,
        "form_b": form_b,
        "form_c": form_c,
    }
    return render(request, '3_form_template.html', context)

def view_a(request):
    form_a = YourFormA(request.POST)
    if form_a.is_valid():
        #do something
        form_a.save()
        return HttpResponseRedirect('your_succsess_url')
    # in case the form is not valid call back the view 
    # which handle the 3 form display
    return 3_forms_view(request)

def view_b(request):
    form_b = YourFormB(request.POST)
    if form_b.is_valid():
        #do something
        form_b.save()
        return HttpResponseRedirect('your_succsess_url')
    # in case the form is not valid call back the view 
    # which handle the 3 form display
    return 3_forms_view(request)

#same thing for view c

3\表单\模板

<form action="{% url 'view_a_url_name' %}" method="post">
  {% csrf_token %}
  {{ form_a.as_p }}
  <button type="submit">Submit A</button>
</form>
<form action="{% url 'view_b_url_name' %}" method="post">
  {% csrf_token %}
  {{ form_b.as_p }}
  <button type="submit">Submit B</button>
</form>
<form action="{% url 'view_c_url_name' %}" method="post">
  {% csrf_token %}
  {{ form_c.as_p }}
  <button type="submit">Submit C</button>
</form>

相关问题 更多 >