Django UpdateView doesn't get the values of the object

1.1k views Asked by At

I have some problems with my UpdateView. I have tried many ways to fix it but failed. So may you can help me: When I click my Update-btn the right html file appears and I can fill out my columns. The Problem is, that there are not the values which should be there! My created object, which I want to update, does not appear. There are just empty columns.

My Model:

class Patient(models.Model):
patientID = models.CharField(max_length=200 , default='Enter PatientID')
birth_date = models.DateField(auto_now_add=False, auto_now=False, default='MM/DD/YYYY')
gender = models.CharField(max_length=200,choices=Gender_Choice, default='UNDEFINED')
height = models.IntegerField( default='[cm]')
weight = models.FloatField(default='[kg]')
BMI = models.CharField(max_length=200, default='-')

def get_absolute_url(self):
    return reverse('member:detail', kwargs={'pk':self.pk})

def __str__(self):
    return self.patientID

class PatientCreateForm(ModelForm):
class Meta:
    model = Patient
    fields = ['patientID', 'birth_date', 'gender', 'height', 'weight', 'BMI']

My View:

class PatientUpdate(UpdateView):
form_class = PatientCreateForm
model = Patient
template_name_suffix = '_update_form'

My URL:

    # /member/patient/2/
url(r'patient/(?P<pk>[0-9]+)/$', views.PatientUpdate.as_view(), name='patient-update'),

patient_update_form.html:

<div class="panel-body">
  <h2>Update patient data</h2>
   <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {% include 'member/form-template.html' %}
      <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
         <button type="submit" class="btn btn-success" value="Update">Update</button>
      </div>
      </div>
   </form>
</div>

1

There are 1 answers

8
Kornikopic On

I presume you wanted the default value to appear in your form.

height = models.IntegerField( default='[cm]')
weight = models.FloatField(default='[kg]')
  1. default should be an integer for height
  2. default should be a float for weight

What you are looking for is placeholder. In your form, you can add a placeholder by overriding the default widget for the fields: https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#overriding-the-default-fields