django - How to change existing code to ModelForm instance
<p>New to Django and this is my first web application.</p>

<p>I'm having trouble with django's ModelForm feature and I wanted to know:</p>

<p>How do I modify my code so that I can create an instance of ModelForm, and specifically, how can I extract the form data to upload to the backend? I will need to reference this instance at a later time to re-populate the same data in an <code>update_profile</code> view but the updation can only happen once the user is logged in (after signup and profile creation).</p>

<p>For the editing section, do I use <code>pk=some_record.pk</code>? Very confused, any help is appreciated.</p>

<p>The model I'm working with <code>CustomerDetail</code> has a foreign key field <code>customer</code> which references the <code>Customer</code> model:</p>

<pre><code>class CustomerDetail(models.Model):
    phone_regex = RegexValidator(regex = r'^\d{10}$', message = "Invalid format! E.g. 4088385778")
    date_regex = RegexValidator(regex = r'^(\d{2})[/.-](\d{2})[/.-](\d{2})$', message = "Invalid format! E.g. 05/16/91")

    customer = models.OneToOneField(Customer,
    on_delete=models.CASCADE,
    primary_key=True,)
    address = models.CharField(max_length=100)
    date_of_birth = models.CharField(validators = [date_regex], max_length = 10, blank = True)
    company = models.CharField(max_length=30)
    home_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True)
    work_phone = models.CharField(validators = [phone_regex], max_length = 10, blank = True)

    def __str__(self):
        return str(self.customer)
</code></pre>

<p>Here is a snippet of <code>views.py</code>:</p>

<pre><code>def create_profile(request):
if request.POST:
    address = request.POST['address']
    date_of_birth = request.POST['date_of_birth']
    company = request.POST['company']
    home_phone = request.POST['home_phone']
    work_phone = request.POST['work_phone']

    custprofdata = CustomerDetail(address = address, date_of_birth = date_of_birth, company = company, home_phone = home_phone, work_phone = work_phone)
    custprofdata.save()

    output = {'address': address, 'dateofbirth': date_of_birth, 'company': company, 'homephone': home_phone, 'workphone': work_phone}

    return render(request, 'newuser/profile_created.html', output)
else:
    return redirect(create_profile)
</code></pre>

<p>And here is a snippet of the form part of the respective <code>create_profile.html</code>:</p>

<pre><code>&lt;form action = "{% url 'create_profile' %}" class="create_profile" role="form" method = "post"&gt;
    {% csrf_token %} 
    &lt;div class="form-group"&gt;
    &lt;label for="address" class="col-md-3 control-label"&gt;Address&lt;/label&gt;
    &lt;div class="col-md-9"&gt;
        &lt;input type="text" class="form-control" name="address" placeholder="777 Park St" /&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
    &lt;label for="date-of-birth" class="col-md-3 control-label"&gt;Date Of Birth&lt;/label&gt;
    &lt;div class="col-md-9"&gt;
        &lt;input type="text" class="form-control" name="date_of_birth" placeholder="09/12/82" /&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
    &lt;label for="company" class="col-md-3 control-label"&gt;Company&lt;/label&gt;
    &lt;div class="col-md-9"&gt;
        &lt;input type="text" class="form-control" name="company" placeholder="Oracle"&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
    &lt;label for="home-phone" class="col-md-3 control-label"&gt;Home Phone&lt;/label&gt;
    &lt;div class="col-md-9"&gt;
        &lt;input type="text" class="form-control" name="home_phone" placeholder="4082992788"&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
    &lt;label for="work-phone" class="col-md-3 control-label"&gt;Work Phone&lt;/label&gt;
    &lt;div class="col-md-9"&gt;
        &lt;input type="text" class="form-control" name="work_phone" placeholder="6690039955"&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
    &lt;div class="col-md-offset-3 col-md-9"&gt;
        &lt;button type = "create" class="btn btn-success" form = "create_profile"&gt; Submit &lt;/button&gt;
    &lt;/div&gt;
    &lt;/div&gt;              
 &lt;/form&gt;
</code></pre>

<p>Implementing a basic ModelForm is just a matter of the following:</p>

<pre><code>from django.forms import ModelForm

from .models import CustomerDetail

class CustomerDetailForm(ModelForm):
    class Meta:
        model = CustomerDetail
        fields = ['address', 'date_of_birth', 'company', 'home_phone', 'work_phone',]
</code></pre>

<p><a href="https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#a-full-example" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/#a-full-example</a></p>

<p>But I suggest you also switch to using a Class Based View (CBV) - the CreateView will do the same as your existing view with much less code, with an implicit ModelForm (which you can customise by providing your own ModelForm class with <code>form_class = YourFormClass</code> if you want).</p>

<p><a href="https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-editing/#createview" rel="nofollow">https://docs.djangoproject.com/en/1.10/ref/class-based-views/generic-editing/#createview</a></p>

<p><a href="https://ccbv.co.uk/projects/Django/1.10/django.views.generic.edit/CreateView/" rel="nofollow">https://ccbv.co.uk/projects/Django/1.10/django.views.generic.edit/CreateView/</a></p>

python django modelform