【发布时间】:2014-02-17 21:39:40
【问题描述】:
如何在 Django 模板中使用 Ajax?
urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^home/$', 'views.index'),)
views.py
from django.template import RequestContext
from django.shortcuts import render_to_response
def index(request):
val = request.POST.get('val', default='text_text')
return render_to_response(
'index.html',
{'text': val,},
context_instance=RequestContext(request))
index.html
<p>text and pictures</p>
<p>text and pictures</p>
<h1>{{ text }}</h1>
<form action="/home/" method="post">
{% csrf_token %}
<input type="text" name="val" id="val" value="{{ val }}"/> <br/>
<input type="submit" value="OK" />
</form>
<p>text and pictures</p>
<p>text and pictures</p>
我如何在您按下 OK 按钮时做到这一点仅更新页面的一部分在标签 h1 之间?
【问题讨论】:
-
您可以阅读此链接以获取简单的 ajax 调用 stackoverflow.com/questions/21259988/…
标签: python html ajax django django-templates