我正在使用django web框架創建日曆網頁。我在頁面上有幾個按鈕,「下一個」和「上一個」按鈕,我想更改視圖上某些事物的值。如何根據當前頁面上的信息重新運行視圖
值的變化取決於當前頁面上的內容,所以我使用ajax從頁面獲取一些信息並將其發送到視圖。這裏的觀點:
def holiday(request):
if request.is_ajax():
# find out if next or previous has been clicked
request_type = request.GET['type']
if request_type == "next":
response_data = {}
# calculate new month and year based off current month and year from view
year = a_new_year
month = a_new_month
if request_type == "previous":
#same again
else:
# page loaded for first time use current date
date_today = datetime.now()
year = date_today.year
month = date_today.month
my_holidays = Holiday.objects.order_by('start_date').filter(
Q(start_date__year=year, start_date__month=month) | Q(end_date__year=year, end_date__month=month)
)
cal = HolidayCalendar(my_holidays, request.user).formatmonth(year, month)
context = {
"calendar": mark_safe(cal),
}
return render(request, "tande/calendar.html", context)
然後,我有這樣的AJAX
$('#next').click(function() {
$.ajax({
url: "{% url 'tande:holiday' %}",
method: 'GET',
data: {
current: $('th.month').html(),
type: "next"
},
success : function (json) {
console.log("hello");
}
});
});
基本上,這是不行的,因爲響應這裏只是頁面的HTML。
如何運行與 1)從模板信息的視圖 - 這是一個表頭的內容 2)什麼按鈕我按下的信息 - 我需要的按鈕的ID
,並得到它渲染?
我不明白你的意思。你*是*重新渲染頁面;但是你的Ajax成功方法並沒有對返回的響應做任何事情,所以它沒有任何意義。 –
是的 - 基本上我不知道現在該做什麼。我設法發送數據到視圖 - 我需要的東西。並運行視圖。我想問題是我現在要做什麼來刷新頁面? @DanielRoseman –
那麼,如果你想頁面完全刷新,我不知道你爲什麼使用Ajax;只是使月份的正常聯繫。否則,您的成功函數需要使用jQuery將結果插入到現有頁面HTML中。 –