在没有Django默认模型形式的情况下,在Django中处理同一页面上的多种形式 [英] Handling Multiple Forms on the Same Page in Django without Django's default model form

查看:29
本文介绍了在没有Django默认模型形式的情况下,在Django中处理同一页面上的多种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Django 在同一页面上开发具有两种形式的网页.任何人都可以填写任何单个表单并提交,然后该数据将保存在数据库中.在这里,我尝试避免使用Django的默认模型形式以及任何类型的js内容.

I am trying to develop a webpage with two forms on the same page with Django. Anyone will able to fillup any single form and submit it, then that data will be saved in the database. Here I try to avoid Django's default model form and any type of js stuff also.

我的 HTML :

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

    <!-- animated css link -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.0.0/animate.min.css"/>

    <!-- font awsome css link -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

    <!-- custom css link -->
    <link rel="stylesheet" type="text/css" href="#">

</head>
<body>

    <div class="row m-0 bg-info">
        <h2 class="mx-auto my-4">Multiple form in one page</h2>
    </div>
    
    <div style="height: 40px;"></div>
    <div class="container-fluid my-5">
        <div class="row m-0 d-flex justify-content-center">

            <div class="col-md-3">
                <h2>Employee Profile</h2>
                <form class="#" method="POST" id="EmployeeProfile">
                    <div class="form-group">
                        <label>Ranks</label>
                        <input type="text" class="form-control" name="ranks" placeholder="Enter ranks">
                    </div>
                    <div class="form-group">
                        <label>salary</label>
                        <input type="text" class="form-control" name="salary" placeholder="Enter salary">
                    </div>

                    <div class="row d-flex justify-content-center">
                        <button type="submit" class="btn btn-primary m-2">Submit</button>
                    </div>
                </form>
            </div>

            <div class="col-md-2"></div>

            <div class="col-md-3">
                <h2>Inspector Profile</h2>
                <form class="#" method="POST" id="InspectorProfile">
                    <div class="form-group">
                        <label>Ranks</label>
                        <input type="text" class="form-control" name="ranks" placeholder="Enter ranks">
                    </div>
                    <div class="form-group">
                        <label>Your email</label>
                        <input type="email" class="form-control" name="email" placeholder="email">
                    </div>

                    <div class="row d-flex justify-content-center">
                        <button type="submit" class="btn btn-primary m-2">Submit</button>
                    </div>
                </form>
            </div>

        </div>
    </div>


</body>
</html>

在这里,如何使用基于函数的视图对 views.py 进行编码,以从此表单中获取数据?以及如何确定哪种形式向我发送了该数据.

Here how can I code my views.py with function based view to get data from this form? and how to determine which form sent me that data.

推荐答案

您可以通过设置不同的表单操作并为类似的垂直表单创建不同的视图来做到这一点在您的 template内:

You can do that by setting different form action and by making different view for perticular form like this inside your template :

 <form method="POST" action="{% url 'form1' %}">
    ........all input fields
    </form>
    
    <form method="POST" action="{% url 'form2' %}">
    ........all input fields
    </form>

在您的 views.py中:

def form1(request):
  if request.method == "POST":
      ...get all values
      return render(request,'forms.html')

def form2(request):
  if request.method == "POST":
      ...get all values
      return render(request,'forms.html')

这篇关于在没有Django默认模型形式的情况下,在Django中处理同一页面上的多种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆