【问题标题】:In Django I cant retrieve the values for choice fields , file fields with this value ={{i.cv} in file field and similarly the choice fields在 Django 中,我无法检索选择字段的值、文件字段中具有此值的文件字段 ={{i.cv} 以及类似的选择字段
【发布时间】:2021-07-30 07:24:24
【问题描述】:

这是我的 edit.html 代码,在从数据库中检索值期间,它只显示像 value={{ i.full_name}} 这样的文本字段,但是当我写 value={{ i.cv}} 时,value= {{ i.status}}, value={{ i.gender}} 它没有显示需要编辑的值我有两个选择字段和一个文件字段。

这是我的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.cv}}, value={{ i.status}}, value={{ i.gender}} 来检索 Upload cv, Gender , Status 的值,而文本字段工作正常我得到的值意味着我可以在我的表单中显示全名的值,这个值={{ i.full_name}} 但不是选择和文件字段我不知道哪里出错了。

【问题讨论】:

    标签: python html django database


    【解决方案1】:

    为您的更新功能

    替换:

    i.full_name = request.POST['full_name']
    

    与:

    i.full_name = request.POST.get('full_name')
    

    你需要对所有字段都这样做,并且更新应该可以工作

    【讨论】:

    • 其他字段与文本字段一样工作只有三个字段不工作上传简历、性别、状态两个选择字段和一个我正在上传 pdf 的字段字段
    • 检查我的 html 是否我做错了什么
    • 您已经发布了相同的帖子。 stackoverflow.com/questions/67438156/…。请从那里遵循并实施提示。请尝试通过解决一个问题并仅发布相关代码部分来更具体,否则很难提供帮助。再次澄清一下:您需要了解如何使用 HTML 标签 'radio' 以及如何使用 Django 标签以及 3. 如何使用 django FileField 和对象 'File '。
    猜你喜欢
    • 1970-01-01
    • 2014-05-10
    • 2012-03-20
    • 1970-01-01
    • 2014-04-11
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多