我在将 jquery mobile 集成到我的 django 项目中时遇到问题。尤其是登录功能似乎无法在 jquery mobile(JQM) 中开箱即用。 JQM 使用 ajax 来处理 post 请求,我想阻止这种情况。在这个网站上http://blog.vrplumber.com/index.php?/archives/2511-Miscellaneous-jQuery-Mobile-+-Django-tips.html
我读到可以通过添加来阻止 JQM 这样做
data-json="false"
但是我该在哪里添加它呢?在模板中还是在 View 中?我尝试了不同的变化,但没有效果。
这是我的登录 View :
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 %}
最佳答案
好吧,我自己想出来了: 只需将此 javascript 片段添加到您的模板头中即可:
<script type="text/javascript">
$(document).bind("mobileinit", function(){
ajaxEnabled:false;
});
</script>
关于jquery - 如何防止jquery mobile在django中使用ajax处理post请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9015930/