如何防止jQuery Mobile在Django中用Ajax处理POST请求
我在把jquery mobile整合到我的django项目中时遇到了一些麻烦。特别是登录功能,似乎和jquery mobile(JQM)不太兼容。JQM使用ajax来处理表单提交的请求,而我想要避免这种情况。在这个网站上 http://blog.vrplumber.com/index.php?/archives/2511-Miscellaneous-jQuery-Mobile-+-Django-tips.html
我看到可以通过添加
data-json="false"
来阻止JQM这样做,但我应该把这个放在哪里呢?是在模板里还是在视图里?我尝试了不同的方式,但都没有效果。
这是我的登录视图:
def login(request):
if request.method == 'POST':
username = request.POST['u']
password = request.POST['p']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth_login(request, user)
msg.append("Hello %s your login was successful"% username)
return HttpResponseRedirect('/profile/')
else:
msg.append("disabled account")
else:
msg.append("invalid login")
return render_to_response('login.html')
模板看起来是这样的...
{% block content %}
<form action="" method="post">{% csrf_token %}
Login: <input type="text" name="u">
<br/>
Password: <input type="password" name="p">
<input type="submit" value="Login">
</form>
{% if errors %}
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<a href="logout"> Logout </a>
{% endblock %}
1 个回答
2
好的,我自己搞定了:只需要把这段JavaScript代码加到你的模板头部就行:
<script type="text/javascript">
$(document).bind("mobileinit", function(){
ajaxEnabled:false;
});
</script>