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"))