Scenario: A user has already completed a form.
What's required: How do you return a completed model form in Views.py? I have followed a few guides on here such as this one with no success. Here is my shortened code containing the relevant sections:
models.py
APPROVAL_CHOICES = [
(None, 'Please select an option'),
("Yes", "Yes"),
("No", "No"),
]
class Direct(models.Model):
def __str__(self):
return self.registered_company_name
class Meta:
verbose_name_plural = 'Company Direct Application'
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, null=True")
registered_company_name = models.CharField(max_length=250, verbose_name="Company")
trading_name = models.CharField(max_length=250)
registered_company_address = models.CharField(max_length=250)
further_details_required = models.CharField(max_length=3, choices=APPROVAL_CHOICES, blank=True, verbose_name="Further Details Required")
forms.py
class CompanyDirectApplication(forms.ModelForm):
class Meta:
model = Direct
fields = ["registered_company_name", "trading_name", "registered_company_address"]
widgets = {
"registered_company_name": TextInput(attrs={'class': 'form-control'}),
"trading_name": TextInput(attrs={'class': 'form-control'}),
"registered_company_address": TextInput(attrs={'class': 'form-control'})
}
views.py
if Direct.objects.filter(further_details_required="Yes", user=request.user).exists():
form = CompanyDirectApplication()
form.instance.user = request.user
return render(request, 'direct_application.html', {'form': form})
#otherwise render a blank model form
else:
form = CompanyDirectApplication()
return render(request, 'direct_application.html', {'form': form})
The HTML template renders the form with a simple {{form.as_p}}
. How do I return a completed model form in Views.py to render in my template {{form.as_p}}
?
CodePudding user response:
You can use instance parameter to initialize the form with object:
directs = Direct.objects.filter(further_details_required="Yes", user=request.user)
if directs.exists():
form = CompanyDirectApplication(instance=directs.first())
else:
form = CompanyDirectApplication()
return render(request, 'direct_application.html', {'form': form})
CodePudding user response:
To return a completed model form in Views.py, you can use the instance
parameter when creating the form. This parameter specifies the model instance that the form should be populated with.
Here is an example of how you can modify your code to return a completed form:
# Get the Direct model instance that you want to populate the form with
direct = Direct.objects.get(further_details_required="Yes", user=request.user)
# Create the form instance, passing the Direct model instance as the `instance` parameter
form = CompanyDirectApplication(instance=direct)
# Pass the form instance to the template when rendering it
return render(request, 'direct_application.html', {'form': form})
In your template, you can then render the form using {{form.as_p}} as usual. The form should be pre-populated with the data from the Direct model instance.
Note: In the example code above, I assumed that the Direct
model instance that you want to use to populate the form exists in the database. If this is not the case, you will need to handle the case where the Direct
model instance does not exist, for example by displaying an error message or redirecting the user to a different page.
CodePudding user response:
Change the view like this. Its will fill the existing data.
if Direct.objects.filter(further_details_required="Yes", user=request.user).exists():
direct = Direct.objects.get(further_details_required="Yes", user=request.user)
form = CompanyDirectApplication(instance=direct)
return render(request, 'direct_application.html', {'form': form})
else:
form = CompanyDirectApplication()
return render(request, 'direct_application.html', {'form': form})