Django & intercooler.js : 使用 Intercooler.js 将变量传递给视图
Django & intercooler.js : Pass a variable to a view with Intercooler.js
我正在尝试将 intercooler.js (https://intercoolerjs.org) 集成到我的项目中,但无法将模板中的变量传递到视图中。
使用下面的代码,我试图在每次点击时显示一个递增 1 的整数。
(a) 模板:
<form id="defaults">
<input name="csrfmiddlewaretoken" type="hidden" value="{{ csrf_token }}">
</form>
<button ic-post-to="{% url 'habitap:click' %}?click_value={{clicks}}" ic-include="#defaults">
{{clicks}}
</button>
(b) 视图
def click(request):
clicks = request.POST.get('click_value', 0)
clicks = clicks + 1
context = {'clicks': clicks}
template = Template("{{clicks}}")
request_context = RequestContext(request, context)
return HttpResponse(template.render(request_context))
谢谢!
这有效 :) :) :)
模板:
<form id="defaults">
<input name="csrfmiddlewaretoken" type="hidden" value="{{ csrf_token }}">
</form>
<button ic-post-to="{% url 'habitap:click' %}?click={{click}}" ic-replace-target="true" ic-include="#defaults">Update Div</button>
观点:
def click(request):
click = int('0' + request.GET.get('click', 0))
click = click + 1
template = Template(
'''
<button ic-post-to="{% url 'habitap:click' %}?click={{click}}" ic-replace-target="true" ic-include="#defaults">
{{click}}
</div>
''')
context = {'click': click}
request_context = RequestContext(request, context)
return HttpResponse(template.render(request_context))
我正在尝试将 intercooler.js (https://intercoolerjs.org) 集成到我的项目中,但无法将模板中的变量传递到视图中。
使用下面的代码,我试图在每次点击时显示一个递增 1 的整数。
(a) 模板:
<form id="defaults">
<input name="csrfmiddlewaretoken" type="hidden" value="{{ csrf_token }}">
</form>
<button ic-post-to="{% url 'habitap:click' %}?click_value={{clicks}}" ic-include="#defaults">
{{clicks}}
</button>
(b) 视图
def click(request):
clicks = request.POST.get('click_value', 0)
clicks = clicks + 1
context = {'clicks': clicks}
template = Template("{{clicks}}")
request_context = RequestContext(request, context)
return HttpResponse(template.render(request_context))
谢谢!
这有效 :) :) :)
模板:
<form id="defaults">
<input name="csrfmiddlewaretoken" type="hidden" value="{{ csrf_token }}">
</form>
<button ic-post-to="{% url 'habitap:click' %}?click={{click}}" ic-replace-target="true" ic-include="#defaults">Update Div</button>
观点:
def click(request):
click = int('0' + request.GET.get('click', 0))
click = click + 1
template = Template(
'''
<button ic-post-to="{% url 'habitap:click' %}?click={{click}}" ic-replace-target="true" ic-include="#defaults">
{{click}}
</div>
''')
context = {'click': click}
request_context = RequestContext(request, context)
return HttpResponse(template.render(request_context))