我的「查看」代碼在代碼中有明顯的重複。有什麼方法可以重構「返回」代碼?重構「查看」代碼
def form_contractor_view(request):
if request.method == 'POST':
form = ContractorForm(request.POST)
if form.is_valid():
form.save()
return redirect('index_view')
else:
return render_to_response(
'form_contractor.html',
{'form': form},
context_instance=RequestContext(request),
)
else:
form = ContractorForm()
return render_to_response(
'form_contractor.html',
{'form': form},
context_instance=RequestContext(request),
)
我非常建議你考慮基於類的視圖。 – Wtower