0

enter image description hereThis is my edit.html code and during retrieving the values from database it shows only the text fields like value={{ i.full_name}} but when I am writing value={{ i.cv}}, value={{ i.status}}, value={{ i.gender}} it does not shows the value which needs to bed edited I have two choice fields and one file field and also when I am uploading a pdf file it is getting stored in media folder and I am showing that file in a table but if a file name which which I have uploaded name java.pdf after uploading it changes to some random name after original name like java_hjh45.pdf why this is happening I don't know

this is my 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> 

this is my views.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')

this is my 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='') 

I want two solutions here

1.I cant retrieve the values of Upload cv, Gender , Status by doing value={{ i.cv}}, value={{ i.status}}, value={{ i.gender}} while the text fields are working fine I am getting the value that means I can display the value of full name with this value={{ i.full_name}} in my form but not the choice and file fields

  1. When I am uploading a file through a form and submitting the file the file is stored in media folder file system but the name changes as soon as I upload for example if my original file name is simple2.pdf it changes to some random name as sample2_fkuTqar.pdf I don't know what's the reason behind this attached an image for this
  • a file gets renamed on upload (like java.pdf -> java_hjh45.pdf) if the java.pdf exists already – Razenstein May 07 '21 at 17:11
  • I would recommend you to add a print(context) in your view before you render the page in order to debug and see in your console what is in your ApplyForm Query object i – Razenstein May 07 '21 at 17:15
  • so like if I have this as original file as simple2.pdf after uploading the file I am getting sample2_fkuTqar.pdf also in database the name is like this and this is what I am displaying in html table – Joy Kumar Chakraborty May 07 '21 at 17:18
  • I would also put the value like this: .... value = "{{ .....}}" – Razenstein May 07 '21 at 17:19
  • yes, FileField is doing that automatically unless you delete simple2.pdf before the (repeated) upload – Razenstein May 07 '21 at 17:21
  • I have put this value = "{{ i.status}}" , value = "{{ i.cv}}" , and value = "{{ i.gender}}" but same I am getting the values of only text fields but these fields are choice fields and file fields see my html once that if I am doing something wrong or not. – Joy Kumar Chakraborty May 07 '21 at 17:36
  • the radio button is like this: . The values need to come from your Choice Options and the "checked" has to be added base on your i.gender like {% if i.gender ..... %}checked{% endif %} – Razenstein May 07 '21 at 17:51
  • the i.cv is an object of type File (see https://docs.djangoproject.com/en/3.2/ref/files/file/) not the filename! Maybe check this question: https://stackoverflow.com/questions/2683621/django-filefield-how-to-return-filename-only-in-template – Razenstein May 07 '21 at 17:57
  • Thanks for sharing this and can you give a example of this in a html format just one example will be okay {% if i.gender ..... %}checked{% endif %} just proper html format – Joy Kumar Chakraborty May 07 '21 at 18:10
  • (see django docs https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#if) – Razenstein May 07 '21 at 18:16
  • Thanks for sharing I am trying this – Joy Kumar Chakraborty May 07 '21 at 18:22
  • Tried this but getting template syntax error Could not parse the remainder: '=='male'' from '=='male'' – Joy Kumar Chakraborty May 07 '21 at 18:40
  • I am getting template syntax error by putting your code Tried this but getting template syntax error Could not parse the remainder: '=='male'' from '=='male'' – Joy Kumar Chakraborty May 08 '21 at 05:04
  • its a typo please put a space between == and 'male': – Razenstein May 08 '21 at 07:03
  • Tried this now errors are not coming but nothing happened its not selected – Joy Kumar Chakraborty May 08 '21 at 07:11
  • this is just an example. You need to check i.gender against your GENDER_CHOICES – Razenstein May 08 '21 at 07:24
  • next step would then be to use for/next django template tags and finally use django forms instead of building the html code manually – Razenstein May 08 '21 at 07:26
  • I have given the gender choices Male and Female in place of checked it didn't worked the choices are here actually I am not using django forms in the model i have given if u use this later here is my gender choices GENDER_CHOICES = ( ('Male', 'Male'), ('Female', 'Female'), ) – Joy Kumar Chakraborty May 08 '21 at 07:28
  • Thanks a lot I have done the gender can u also explain the same thing for the status choice field with this if statement – Joy Kumar Chakraborty May 08 '21 at 16:11
  • I only need solution for this dropdown field I have done the gender thanks for sharing the code – Joy Kumar Chakraborty May 10 '21 at 12:39

0 Answers0