python - 如何在 django 中创建员工多重体验

如何在 Django 中创建多重体验

这是 Model.py 文件代码

from django.db import models

# Create your models here.
class Detail(models.Model):
    first_name = models.CharField(max_length=96)
    last_name = models.CharField(max_length=96)
    email_id = models.CharField(max_length=96)
    mobile_no = models.CharField(max_length=96)
    dob = models.CharField(max_length=96)
    qualification = models.CharField(max_length=96)
    

    def __str__(self):
        return self.first_name

class Experince(models.Model):
    detail = models.ForeignKey(Detail, on_delete = models.CASCADE)
    organization = models.CharField(max_length=96)
    designation = models.CharField(max_length=96)
    date_from = models.CharField(max_length=96)
    date_to = models.CharField(max_length=96)

    def __str__(self):
        return self.organization

这是 View.py 文件代码:

from django.shortcuts import render
from .models import *
#from .forms import *

# Create your views here.



def home(request, id):
    if request.method == 'POST':
        Detail(first_name = request.POST['first_name'],
        last_name = request.POST['last_name'],
        email_id = request.POST['email_id'],
        mobile_no = request.POST['mobile_no'],
        dob = request.POST['dob'],
        qualification = request.POST['qualification']).save()
        Experince(organization = request.POST['organization'],
        designation = request.POST['designation'],
        date_from = request.POST['date_from'],
        date_to = request.POST['date_to']).save()
    
        
    return render(request,"home.html")

这是我的html页面:

<!DOCTYPE html>
<html>
  
<head>
  <title>test page</title>
  <link rel="stylesheet" href=
"https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
    integrity=
"sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
    crossorigin="anonymous">
  
  <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
  </script>
  <script src=
"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js">
  </script>
  <script src=
"https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js">
  </script>
  
  <script>
    $(document).ready(function () {
  
      // Denotes total number of rows
      var rowIdx = 0;
  
      // jQuery button click event to add a row
      $('#addBtn').on('click', function () {
  
        // Adding a row inside the tbody.
        $('#tbody').append(`<tr id="R${++rowIdx}">
             <td class="row-index text-center">
             <p>${rowIdx}</p>
             </td>
            <td class="row-index text-center">
              <input type="text" name="organization">
            </td>
            <td class="row-index text-center">
              <input type="text" name="designation">
            </td>
            <td class="row-index text-center">
              <input type="date" name="date_from">
            </td>
            <td class="row-index text-center">
              <input type="date" name="date_to">
            </td>
            <td class="text-center">
                <button class="btn btn-danger remove"
                  type="button">Remove</button>
                </td>
              </tr>`);
      });
  
      // jQuery button click event to remove a row.
      $('#tbody').on('click', '.remove', function () {
  
        // Getting all the rows next to the row
        // containing the clicked button
        var child = $(this).closest('tr').nextAll();
  
        // Iterating across all the rows 
        // obtained to change the index
        child.each(function () {
  
          // Getting <tr> id.
          var id = $(this).attr('id');
  
          // Getting the <p> inside the .row-index class.
          var idx = $(this).children('.row-index').children('p');
  
          // Gets the row number from <tr> id.
          var dig = parseInt(id.substring(1));
  
          // Modifying row index.
          idx.html(`Row ${dig - 1}`);
  
          // Modifying row id.
          $(this).attr('id', `R${dig - 1}`);
        });
  
        // Removing the current row.
        $(this).closest('tr').remove();
  
        // Decreasing total number of rows by 1.
        rowIdx--;
      });
    });
  </script>
    <title>Hello, world!</title>
    
  </head>
  <body>
    <h1>Hello, world!</h1>
    <div class="container">
    <form method="post">
      {% csrf_token %}
        <div class="row pt-3">
        <div class="col-md-6">
          <input type="text" class="form-control" placeholder="first name" name="first_name">
        </div>
        <div class="col-md-6 " >
          <input type="text" class="form-control" placeholder="last name" name="last_name">
        </div>
    </div>
    <div class="row pt-5">
        <div class="col-md-4">
          <input type="email" class="form-control" placeholder="Email Address" name="email_id">
        </div>
        <div class="col-md-4">
          <input type="text" class="form-control" placeholder="Mobile No" name="mobile_no">
        </div>
        <div class="col-md-4">
          <input type="date" class="form-control" name="dob">
        </div>
        </div>
        <h3>Qualification</h3>
          <select id="inputState" class="form-select" name="qualification">
            <option>Choose...</option>
            <option>B.A</option>
            <option>B.tech.</option>
            <option>BBA</option>
            <option>BCA</option>
            <option>M.tech</option>
            <option>MBA</option>
            <option>MCA</option>
            <option>Other</option>
          </select>
          <div class="pt-3">
            <table class="table table-bordered">
              <thead>
                <tr>
                  <th>S.N</th>
                  <th class="text-center">Organization Name </th>
                  <th class="text-center">Designation</th>
                  <th class="text-center">From </th>
                  <th class="text-center">To</th>
                  <th class="text-center">Remove Row</th>
                </tr>
              </thead>
              <tbody id="tbody">
        
              </tbody>
            </table>
          <button class="btn btn-md btn-primary" 
          id="addBtn" type="button">
        Add experince
      </button>
        </div>
            
            
        <div class="col-12 pt-3">
          <input type="submit" class="btn btn-primary" value="Submit">
        </div>
      </form>
     
      </div>
      
<!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
    
    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js" integrity="sha384-7+zCNj/IqJ95wo16oMtfsKbZ9ccEh31eOz1HGyDuCQ6wgnyJNSYdrPa03rtR1zdB" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js" integrity="sha384-QJHtvGhmr9XOIpI6YVutG+2QOK9T+ZnN4kzFN1RtK3zEFEIsxhlmWl5/YESvpZ13" crossorigin="anonymous"></script>
    -->
  </body>
</html>

我有一个名为 Experince 的模型,它与 Detail 模型有一个外国关系。我需要使用体验模型创建多个体验。假设如果计数为 3,那么它应该在数据库中保存 3 次体验。

回答1

我认为这里有很多问题。如果我理解得很好,您有可以拥有多种体验的用户吗?如果这是真的,那么用户是一个主模型,体验是一个细节模型,并且关系应该是相反的(体验与用户模型有关系)。还有其他可能性,但这是最简单的一种。

比,您没有充分利用 django 模板。无需在视图中分配所有字段,大部分都可以直接完成。

最后,如果您希望在同一页面上添加详细模型(体验)的多个实例,您可能需要查看可以轻松完成此操作的表单集。

希望这可以帮助。

相似文章

随机推荐

最新文章