Hi when I'm doing ajax call, I'm getting this in console:
嗨,当我正在进行ajax调用时,我在控制台中得到这个:
POST http://127.0.0.1:8000/registration/check/username/ 500 (INTERNAL SERVER ERROR)
Also when I click on this link, I'm getting this:
当我点击这个链接时,我得到了这个:
DoesNotExist at /registration/check/username/
User matching query does not exist.
Request Method: POST
Request URL: http://127.0.0.1:8000/registration/check/username/
Django Version: 1.3
Exception Type: DoesNotExist
JQuery:
JQuery的:
function check_username() {
$("#id_username").change(function() {
var user = $("#id_username").val();
var status = $("#id_username").nextAll(".status").first().empty();
var checking = '<img src="/site_media/images/loader.gif" align="absmiddle"> Checking availability...';
var success = '<img src="/site_media/images/tick.gif" align="absmiddle">';
var e_length = '<p>User name have to be longer</p>';
if (user.length >= 3) {
status.append(checking);
$.ajax({
url: "/registration/check/username/",
type: "POST",
data: { username : $("#id_username").val() },
dataType: "text",
success: function(msg) {
if (msg == '1') {
status.append(success);
}
else {
status.append("This username is already in use");
}
}
});
}
else if (user.length <= 3 && user.length != 0) {
status.append(e_length);
}
else {
status;
}
});
}
Html:
HTML:
{% block main-menu %}
<div class="contentarea">
<form method="post" action="">{% csrf_token %}
<ul id="reg-form">
<li>
<label for="id_username">Username:</label>
<input id="id_username" type="text" name="username" maxlength="30" />
<div class="status"></div>
</li>
Urls.py:
Urls.py:
...
(r'^registration/check/([\w|\W]+)/$', register_check),
...
Views.py:
Views.py:
@csrf_exempt
def register_check(request, variable):
if request.is_ajax():
if variable == 'username':
user = User.objects.get(username__exact = request.POST['username']);
if user:
msg = "1"
else:
msg = '0'
return HttpResponse(msg)
else:
return HttpResponse("0")
1 个解决方案
#1
2
Generally use strings for the urls.py
. Instead of using
通常使用urls.py的字符串。而不是使用
(r'^registration/check/([\w|\W]+)/$', register_check),
use
使用
(r'^registration/check/([\w|\W]+)/$', "register_check"),
and the first should be the path to your views file.
第一个应该是您的视图文件的路径。
update
更新
According to Django's 1.4 website here, you need to add the csrf token to the request in the headers. I didn't read the entire chunk of code though.
根据Django的1.4网站,你需要在标题中的请求中添加csrf标记。我没有阅读整个代码块。
#1
2
Generally use strings for the urls.py
. Instead of using
通常使用urls.py的字符串。而不是使用
(r'^registration/check/([\w|\W]+)/$', register_check),
use
使用
(r'^registration/check/([\w|\W]+)/$', "register_check"),
and the first should be the path to your views file.
第一个应该是您的视图文件的路径。
update
更新
According to Django's 1.4 website here, you need to add the csrf token to the request in the headers. I didn't read the entire chunk of code though.
根据Django的1.4网站,你需要在标题中的请求中添加csrf标记。我没有阅读整个代码块。