**方案:**用户已完成表单。
**要求:**如何在www.example.com中返回完整的模型表单Views.py?我在这里遵循了一些指南,如this,但没有成功。以下是我的简短代码,包含相关部分:
型号.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, hoices=APPROVAL_CHOICES, blank=True, verbose_name="Further Details Required")
表单.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'})
}
查看次数.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})
HTML模板使用简单的{{form.as_p}}
呈现表单。如何返回www.example.com中的完整模型表单Views.py以在模板{{form.as_p}}
中呈现?
2条答案
按热度按时间6tr1vspr1#
可以使用示例参数初始化具有对象的窗体:
zwghvu4y2#
像这样更改视图。它将填充现有数据。