I'm hoping someone can help me here. I can't get make a successful ajax call using django, jquery and the jquery form validation plugin. I'm trying to pass a form email address to a view method to see if it exists in db. But server response is 404 on ajax request. In Firebug i can see the request being sent appears to be formatted properly.
我希望有人可以帮助我。我无法使用django,jquery和jquery表单验证插件进行成功的ajax调用。我正在尝试将表单电子邮件地址传递给视图方法,以查看它是否存在于db中。但是ajax请求的服务器响应是404。在Firebug中,我可以看到正在发送的请求似乎格式正确。
request sent is: http://127.0.0.1:8000/xEmailExists/?email=joeblow%40test.cc
请求发送的是:http://127.0.0.1:8000 / xEmailExists/?email = joeblow%40test.cc
urls.py has: (r'^xEmailExists/(?P\d+)$', 'hwa.website.views.root.xEmailExists'),
urls.py有:(r'^ xEmailExists /(?P \ d +)$','hwa.website.views.root.xEmailExists'),
and my hwa.website.views.root view file has the following method signature: def xEmailExists(request, email):
我的hwa.website.views.root视图文件具有以下方法签名:def xEmailExists(request,email):
I'm using Django 1.1 Bet
我正在使用Django 1.1 Bet
2 个解决方案
#1
That looks like a problem with your URL config. Does accessing that URL interactively (i.e. try navigating to it manually) cause a 404?
这似乎是您的URL配置问题。 404是否以交互方式访问该URL(即尝试手动导航)会导致404?
There's not really anything magical about URLS for Ajax requests, they're still just URLs - debug them as you would any URL in a Django application.
关于Ajax请求的URLS并没有什么神奇之处,它们仍然只是URL - 像在Django应用程序中的任何URL一样调试它们。
Unless I'm really sleep-deprived r'^xEmailExists/(?P\d+)$'
has a couple of problems:
除非我真的睡不着,否则r'^ xEmailExists /(?P \ d +)$'有几个问题:
- You're using the named URL pattern without a name - replace
?P
with?P<email>
. - You don't want
\d+
- that'll match 1 or more digits (i.e. 0-9). You probably want to match all characters for the sake of e-mail validation. - Also, what's the point of passing the e-mail as a query string - why not just do
http://127.0.0.1:8000/xEmailExists/joeblow%40test.cc
?
您正在使用没有名称的命名URL模式 - 将?P替换为?P
你不希望\ d + - 那将匹配1位或更多位数(即0-9)。您可能希望匹配所有字符以便进行电子邮件验证。
另外,将电子邮件作为查询字符串传递的重点是什么 - 为什么不执行http://127.0.0.1:8000/xEmailExists/joeblow%40test.cc?
Hope that's some help! Apologies if I screwed up the regex matching - it's one of those things I tend to have to fiddle with to get right, and I've not tested my response.
希望有所帮助!如果我把正则表达式匹配搞砸了道歉 - 这是我倾向于纠正的事情之一,我没有测试我的回答。
#2
As Dominic said, the problem is in your urls.py
, but I'm not sure he has the right solution.
正如Dominic所说,问题在于你的urls.py,但我不确定他是否有正确的解决方案。
First, fix your urls.py
to be the following:
首先,修复您的urls.py如下:
(r'^xEmailExists/$', 'hwa.website.views.root.xEmailExists'),
Then, in your xEmailExists
view, you get the email address by using request.GET
:
然后,在xEmailExists视图中,使用request.GET获取电子邮件地址:
def xEmailExists(request):
email = request.GET.get('email', '')
# check if email exists and return...
This will pull the ?email=test@example.com
out of your query string. Using GET.get('email', '')
returns a default (the empty string) if 'email'
is not in the query string, but if you're sure it'll be there GET['email']
will also work.
这会将?email = test@example.com从查询字符串中拉出来。如果'email'不在查询字符串中,则使用GET.get('email','')返回默认值(空字符串),但如果您确定它将在那里,那么GET ['email']也会工作。
#1
That looks like a problem with your URL config. Does accessing that URL interactively (i.e. try navigating to it manually) cause a 404?
这似乎是您的URL配置问题。 404是否以交互方式访问该URL(即尝试手动导航)会导致404?
There's not really anything magical about URLS for Ajax requests, they're still just URLs - debug them as you would any URL in a Django application.
关于Ajax请求的URLS并没有什么神奇之处,它们仍然只是URL - 像在Django应用程序中的任何URL一样调试它们。
Unless I'm really sleep-deprived r'^xEmailExists/(?P\d+)$'
has a couple of problems:
除非我真的睡不着,否则r'^ xEmailExists /(?P \ d +)$'有几个问题:
- You're using the named URL pattern without a name - replace
?P
with?P<email>
. - You don't want
\d+
- that'll match 1 or more digits (i.e. 0-9). You probably want to match all characters for the sake of e-mail validation. - Also, what's the point of passing the e-mail as a query string - why not just do
http://127.0.0.1:8000/xEmailExists/joeblow%40test.cc
?
您正在使用没有名称的命名URL模式 - 将?P替换为?P
你不希望\ d + - 那将匹配1位或更多位数(即0-9)。您可能希望匹配所有字符以便进行电子邮件验证。
另外,将电子邮件作为查询字符串传递的重点是什么 - 为什么不执行http://127.0.0.1:8000/xEmailExists/joeblow%40test.cc?
Hope that's some help! Apologies if I screwed up the regex matching - it's one of those things I tend to have to fiddle with to get right, and I've not tested my response.
希望有所帮助!如果我把正则表达式匹配搞砸了道歉 - 这是我倾向于纠正的事情之一,我没有测试我的回答。
#2
As Dominic said, the problem is in your urls.py
, but I'm not sure he has the right solution.
正如Dominic所说,问题在于你的urls.py,但我不确定他是否有正确的解决方案。
First, fix your urls.py
to be the following:
首先,修复您的urls.py如下:
(r'^xEmailExists/$', 'hwa.website.views.root.xEmailExists'),
Then, in your xEmailExists
view, you get the email address by using request.GET
:
然后,在xEmailExists视图中,使用request.GET获取电子邮件地址:
def xEmailExists(request):
email = request.GET.get('email', '')
# check if email exists and return...
This will pull the ?email=test@example.com
out of your query string. Using GET.get('email', '')
returns a default (the empty string) if 'email'
is not in the query string, but if you're sure it'll be there GET['email']
will also work.
这会将?email = test@example.com从查询字符串中拉出来。如果'email'不在查询字符串中,则使用GET.get('email','')返回默认值(空字符串),但如果您确定它将在那里,那么GET ['email']也会工作。