0

I am new to Django and programming in general. I am trying to generate a list of records from a database but with two fields that can be edited. In the browser it should show a line with the fields: clientcode, clientname, Reason, comment Name and description come from the model and are a reference. The user should only be able to capture reason and comments

I have created a forms.py file and a ModelForm. My issue is how do I pass through an individual object. For this example I've limited my dataset to 10 records In my view file

def home(request):
    if request.method == 'GET':
        nca = NcaRe.objects.all()[:10]
        form = NcaReForm(instance= <what should go in here> )
        return render(request, 'NCAComments/home.html', {'form': form, 'nca': nca})
    else:
        pass

In my model I have a field called primarykey. I'm not sure how to pass this to the form so that I only bring in that record. I have tried looking at the documentation but have not been able to follow it.

My Model py.

from django.db import models


class NcaRe(models.Model):
    primarykey = models.IntegerField(blank=True, null=False, primary_key=True)
    clientcode = models.CharField(db_column='ClientCode', max_length=200, blank=True, null=True) 
    clientname = models.CharField(db_column='ClientName', max_length=510, blank=True, null=True)
    reason = models.TextField(blank=True, null=True)
    comment = models.TextField(blank=True, null=True)  
    class Meta:
        db_table = 'NCA_RE'

Forms.py

from django.forms import ModelForm
from .models import NcaRe

class NcaReForm(ModelForm):
    class Meta:
        model = NcaRe
        fields = ['reason', 'comment']

In html I am trying to loop through and pass the form

{% for n in nca %}
<p> {{n.clientcode}}</p>
<form>
    {% csrf_token %}
    {{ form }}
</form>
{% endfor %}
2
  • Could you edit your question to include forms.py and models.py in it? Commented May 17, 2020 at 4:34
  • Edited to include the forms, models and what I am trying to achieve with the html Commented May 17, 2020 at 4:44

1 Answer 1

1

In general, you need to just return empty form if the method of request if GET like as form(). I write below sample code that you can do your calculation in after form validation form.is_valid()

views.py

from django.shortcuts import render
from testPhilip.forms import NcaReForm
from testPhilip.models import NcaRe


def home(request):
    if request.method == 'GET':
        nca = NcaRe.objects.all()[:10]
        form = NcaReForm()

    elif request.method == 'POST':
        form = NcaReForm(request.POST)
        if form.is_valid():
            # process the data in form.cleaned_data as required
            # ...
            # redirect to a new URL:

    return render(request, 'testPhilip/home.html', {'form': form, 'nca': nca})

You can retrieve the data after form validation in a cleaned format like this: comment = form.cleaned_data['comment']

Update:

If you want to populate your form fields with values from database or any default values, you can pass them in the 'GET' section as below:

nca_object=NcaRe.objects.get(pk=nca_id)
form=NcaReForm({
    'comment':nca_object.comment,
    'reason':nca_object.reason,
})

For more information about writing forms refer to Django forms doc

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. In this case I am looking more for an update/put option. Reason and comment could already be populated so I would want them to be returned and updated. That's why I am looking to return the instance in the loop. Its from an existing database table so I have initial values in many instances.
@PhilipSeimenis I updated the answer to have your request.
I can get this working if I pass through the primary key as an instance. When I try the above code I am getting errors. The problem is I have multiple forms on the same page like a list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.