1
我在django中有兩個視圖:一個加載頁面,另一個保存表單。我已經分開了這兩個,所以我可以留在同一頁面上,而不刷新頁面導致重新提交。在下一個視圖中獲取如何使用HttpRedirect調用視圖
保存表單的創建視圖重定向回加載頁面的視圖HttpResponseRedirect
。
在加載頁面視圖中,如果視圖是通過重定向創建視圖調用的,是否有可能獲得該視圖?
所以:
def holiday(request, value=None, year=None, month=None):
if request.method == "GET":
# set some variables for the view
year = something
month = something_else
else:
# get if the view was executed from a redirect
# set year and month to be something2 and something_else2
# calendar
holidayform = HolidayForm(request.POST or None, request.FILES or None)
context = {
"holidayform": holidayform,
"calendar": mark_safe(cal),
"year": year,
"month": month,
}
return render(request, "tande/calendar.html", context)
然後,節省的形式觀點:
def create_holiday(request):
overlapping_dates = False
if request.method == "POST":
#save the form... or don't
return HttpResponseRedirect(reverse("tande:holiday"))