首页 > 解决方案 > 在 Django 中,我无法从数据库中的 edit.html 表单中检索 Upload cv、Status、gender 的值,但我可以检索其他字段值

问题描述

在此处输入图像描述这是我的 edit.html 代码,在从数据库中检索值期间,它仅显示诸如 value={{ i.full_name}} 之类的文本字段,但是当我编写 value={{ i.cv}} 时,值={{ i.status}}, value={{ i.gender}} 它没有显示需要编辑的值我有两个选择字段和一个文件字段,而且当我上传 pdf 文件时它正在获取存储在媒体文件夹中,我在表格中显示该文件,但是如果我上传的文件名在上传后命名为 java.pdf,在原始名称之后更改为一些随机名称,如 java_hjh45.pdf 为什么会发生这种情况我不知道

这是我的edit.html

<section class="site-section">
  <div class="container">
    <div class="row">
      <div class="col-lg-12 mb-5">
        <h2 class="mb-4 text-center">Update Candidate Details</h2>
          
        <form method="POST" action="/update/ {{i.id}}/"  enctype="multipart/form-data" class="p-4 border rounded" onsubmit="myFunction()" >
        {%  csrf_token %}
          {% comment %} <input type="hidden" name="csrfmiddlewaretoken" value="UabxqpD8HGPOu1ZSFnIHAPbMtRgWBAnVHEs8bLDx0HnxN6uhG3LyYvZShvcx1ekn"> {% endcomment %}
          
          <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="full_name">Full Name :</label>
              <input type="text" class="form-control" value ={{ i.full_name}} name="full_name" id="id_full_name" placeholder="Enter First Name">
            </div>
          </div>   

          <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="recruiter_name">Recruiter Name :</label>
              <input type="text" class="form-control"  value ={{ i.recruiter_name }} name="recruiter_name" id="id_recruiter_name" placeholder="Enter Recruiter Name">
            </div>
          </div>     
  

          {% comment %} <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
            <label class="text-black" for="id_last_name">Last Name :</label>
              <input type="text" class="form-control" name="last_name" id="id_last_name" placeholder="Enter Last Name">
            </div>
          </div> {% endcomment %}
        
          <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="email">Email :</label>
              <input type="email" class="form-control"  value ={{i.email }} name="email" id="id_email" placeholder="Enter Email">
            </div>
          </div>

          <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="noticeperiod">Notice Period (in Days) :</label>
              <input type="text" class="form-control"  value ={{i.noticeperiod }} name="noticeperiod" id="notice_period" placeholder="Notice Period">
            </div>
          </div>

          <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="preferredlocation">Current Location :</label>
              <input type="text" class="form-control"  value ={{i.preferredlocation}} name="preferredlocation" id="preferred_location" placeholder="Enter Current Location">
            </div>
          </div>

            <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="expectedlocation">Expected Location :</label>
              <input type="text" class="form-control"  value ={{i.expectedlocation}} name="expectedlocation" id="expected_location" placeholder="Enter Multiple Locations seperated by comma">
            </div>
          </div>

            <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="currentctc">Current CTC (Per Annum) :</label>
              <input type="text" class="form-control"   value ={{i.currentctc }} name="currentctc" id="current_ctc" placeholder="Current CTC">
            </div>
          </div>

            <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="expectedctc">Expected CTC (Per Annum) : </label>
              <input type="text" class="form-control"  value ={{i.expectedctc }} name="expectedctc" id="expected_ctc" placeholder="Expected CTC">
            </div>
          </div>
          
           <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" for="status">Status : </label> </br>

              <select name="status" id="status" class="form-control"  value ={{i.status }}>
                <option value="CV Received">CV Received</option>
                <option value="Submitted to Client">Submitted to Client</option>
                <option value="Rejected">Rejected</option>
                <option value="Offered">Offered</option>
                <option value="Joined">Joined</option>
                <option value="Closed">Closed</option>
                <option value="Payment Received">Payment Received</option>
                
              </select>
              {% comment %} <input type="text" class="form-control" name="status" id="status1" placeholder="Enter Application Status"> {% endcomment %}
            </div>
          </div>

          <div class="row form-group">
            <div class="col-md-12 mb-3 mb-md-0">
              <label class="text-black" >Upload CV</label></br>
              <input type="file"  name="cv" id="cv1"   value ={{ i.cv}} >
            </div>
          </div>


      
          <div class="row form-group mb-4">
            <div class="col-md-12 mb-3 mb-md-0">
              <div class="form-check form-check-inline">
              
                <input class="form-check-input" type="radio" name="gender" id="male" value={{i.gender}}>
                
                <label class="form-check-label" for="male">Male</label>
              </div>
              <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="gender" id="female" value={{i.gender}}>
                <label class="form-check-label" for="female">Female</label>
              </div>
            </div>
          </div>
        
          <div class="row form-group">
            <div class="col-md-12">
              <input type="submit" value="Update" id ="submit" class="btn px-4 btn-primary text-white">

               {% comment %} <a href="{% url 'jobapp:jobconfirm' %}" class="btn btn-warning btn-block btn-lg">Sign Up</a> {% endcomment %}
            </div>
          </div> 

这是我的意见.py

#delete
def delete(request, id):
    i = ApplyForm.objects.get(id = id)
    i.delete()
    return redirect('account:dboard')

#edit
def edit(request, id):
    i = ApplyForm.objects.get(id = id)
    context = {
        'i' : i
    }
    return render(request, 'account/edit.html', context)
    
#update
def update(request, id):
    i = ApplyForm.objects.get(id = id)
    i.full_name = request.POST['full_name']
    i.email = request.POST['email']
    i.noticeperiod = request.POST['noticeperiod']
    i.preferredlocation = request.POST['preferredlocation']
    i.expectedlocation = request.POST['expectedlocation']
    i.currentctc = request.POST['currentctc']
    i.expectedctc= request.POST['expectedctc']
    i.recruiter_name = request.POST['recruiter_name']
    i.status = request.POST['status']
    i.gender = request.POST['gender']
    i.cv = request.FILES['cv']
    i.save()
    return redirect('account:dboard')

这是我的models.py

class ApplyForm(models.Model):
    full_name = models.CharField(max_length=300)
    email = models.EmailField(max_length=300)
    noticeperiod = models.CharField(max_length = 300)
    preferredlocation = models.CharField(max_length=300)
    expectedlocation = models.CharField(max_length=300)
    currentctc = models.CharField(max_length=300)
    expectedctc = models.CharField(max_length=300)
    recruiter_name = models.CharField(max_length = 300, null=True)
    status = models.CharField(max_length = 300,  null=True, choices= CATEGORY_CHOICES )
    gender = models.CharField(max_length=10, choices=GENDER_CHOICES)
    cv = models.FileField(upload_to='') 

我在这里想要两个解决方案

1.当文本字段为工作正常我得到的值意味着我可以在我的表单中使用这个 value={{ i.full_name}} 显示全名的值,但不能在选择和文件字段中显示

  1. 当我通过表单上传文件并提交文件时,文件存储在媒体文件夹文件系统中,但名称会在我上传后立即更改,例如,如果我的原始文件名是 simple2.pdf,它会更改为一些随机名称,例如 sample2_fkuTqar .pdf 我不知道这背后的原因是什么,为此附上了一张图片

标签: pythonhtmldjangodatabase

解决方案


推荐阅读