$('#id_button).click(function(){
$('body').css({'background-color': 'dark-gray'})
$.post({% url 'change-background-color' %}, {color: 'dark-gray'}, function(){
location.reload() // to refresh the page with new background color
})
})
# urls.py
add the following path
path('change-background-color/', views.change_background_color, name='change-background-color'),
# views.py
add the following view
def change_background_color(request):
color = request.POST.get('color')
# you could save the input to a model BackgroundColor as an instance or
update a current record.
# creating an instance
BackgroundColor.objects.create(bg_color=color)
return JsonResponse({'response': 'successfully changed color'})
这是很容易做到的。按钮单击可以连接到jquery单击函数,该函数还包括一个ajax调用:
现在只剩下确保html的背景颜色设置为一个变量,该变量引用在Ajax调用中保存颜色的模型实例
相关问题 更多 >
编程相关推荐