2 回答

TA貢獻1856條經(jīng)驗 獲得超5個贊
我沒有找到任何代碼來顯示 html 中的錯誤。根據(jù)views中的函數(shù),如果表單無效,則使用表單渲染頁面。嘗試在html文件中添加{{form.errors}}看看是否有錯誤?

TA貢獻2051條經(jīng)驗 獲得超10個贊
我設(shè)法解決了它。
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})
問題是它無法捕獲“創(chuàng)建者”和“上次更新者”字段的用戶電子郵件。
這是通過使用以下方法解決的:
org = form.save(commit=False)
org.created_by = request.user
org.last_updated_by = request.user
添加回答
舉報