我在我的django應用程序中有以下視圖。如何使用Django中的裝飾器保存發佈數據
def edit(request, collection_id):
collection = get_object_or_404(Collection, pk=collection_id)
form = CollectionForm(instance=collection)
if request.method == 'POST':
if 'comicrequest' in request.POST:
c = SubmissionLog(name=request.POST['newtitle'], sub_date=datetime.now())
c.save()
else:
form = CollectionForm(request.POST, instance=collection)
if form.is_valid():
update_collection = form.save()
return redirect('viewer:viewer', collection_id=update_collection.id)
return render(request, 'viewer/edit.html', {'form': form})
它顯示一個表單,允許您編輯一組圖像。我的html頁腳包含一個表單,允許您從管理員請求新的圖像源。它提交給與CollectionForm不同的數據模型。由於這是在每個視圖的底部,我想提取代碼的第5-7行並將其變成裝飾器。這是可能的,如果是這樣,我該怎麼做呢?
爲什麼不爲您的頁腳的表單提供單獨的視圖? – Ngenator 2014-10-10 18:31:30