views:

486

answers:

2

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.

request sent is: http://127.0.0.1:8000/xEmailExists/?email=joeblow%40test.cc

urls.py has: (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):

I'm using Django 1.1 Bet

A: 

That looks like a problem with your URL config. Does accessing that URL interactively (i.e. try navigating to it manually) cause a 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.

Unless I'm really sleep-deprived r'^xEmailExists/(?P\d+)$' has a couple of problems:

  1. You're using the named URL pattern without a name - replace ?P with ?P<email>.
  2. 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.
  3. 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?

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.

Dominic Rodger
Dominic is right, it's a problem in urls.py. The url pattern ends with '$' right after the email pattern, which doesn't allow for the trailing slash after email. @john: change your url pattern to r'^xEmailExists/(?P\d+)/$'
Van Gale
@Van Gale - am I confused or does \d+ not match one or more digits?
Dominic Rodger
@Dominic yes, but the $ immediately after \d+ means you can't have a trailing slash... which he does in his "sent request"
Van Gale
So surely r'^xEmailExists/(?P\d+)/$' won't work - since e-mail addresses aren't just a bunch of numbers?
Dominic Rodger
@Dominic ah, I just re-read your second point and you are correct on that as well, but now I notice he's actually including the email address as a query string so it won't be matched anyway. @john: your URL pattern should look like r'^xEmailExists/' and eliminate the email argument to your view. If you pass email as a query string then you'll access it using request.GET['email'] instead.
Van Gale
thanks guys. i've got this working now due to your help. I'm not a regex person. trying to get up to speed due to Django. Anyway the problem was the \d+. I removed it and it worked fine. I will say i did have the name key in url pattern. it didn't come through due to angle brackets? Anyway, i did not have to use request.GET['email']. it worked this way: urls.py (r'^xEmailExists/(?P<email>)$','hwa.website.views.root.xEmailExists'),with the following view method signature:def xEmailExists(request, email):
A: 

As Dominic said, the problem is in your urls.py, but I'm not sure he has the right solution.

First, fix your urls.py to be the following:

(r'^xEmailExists/$', 'hwa.website.views.root.xEmailExists'),

Then, in your xEmailExists view, you get the email address by using request.GET:

def xEmailExists(request):
    email = request.GET.get('email', '')
    # check if email exists and return...

This will pull the [email protected] 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.

tghw