2 回答
TA贡献1856条经验 获得超5个赞
我没有找到任何代码来显示 html 中的错误。根据views中的函数,如果表单无效,则使用表单渲染页面。尝试在html文件中添加{{form.errors}}看看是否有错误?
TA贡献2051条经验 获得超10个赞
我设法解决了它。
views.py
@login_required
def organization_add(request):
if request.method == 'POST':
form = OrganizationAddForm(request.POST)
if form.is_valid():
form.organization_code = form.cleaned_data['organization_code']
form.company_name = form.cleaned_data['company_name']
form.legal_name = form.cleaned_data['legal_name']
form.business_registration_no = form.cleaned_data['business_registration_no']
form.vat_registration_no = form.cleaned_data['vat_registration_no']
form.industry_distribution = form.cleaned_data['industry_distribution']
form.industry_education = form.cleaned_data['industry_education']
form.industry_healthcare = form.cleaned_data['industry_healthcare']
form.industry_manufacturing = form.cleaned_data['industry_manufacturing']
form.industry_retail = form.cleaned_data['industry_retail']
form.industry_services = form.cleaned_data['industry_services']
form.effective_start_date = form.cleaned_data['effective_start_date']
form.effective_end_date = form.cleaned_data['effective_end_date']
org = form.save(commit=False)
org.created_by = request.user
org.last_updated_by = request.user
org.save()
return redirect('organizations_settings')
else:
form = OrganizationAddForm()
return render(request, 'settings/add_organization.html', {'form': form})
问题是它无法捕获“创建者”和“上次更新者”字段的用户电子邮件。
这是通过使用以下方法解决的:
org = form.save(commit=False)
org.created_by = request.user
org.last_updated_by = request.user
添加回答
举报