URLs and Regex Question for passing email address?

1,071 views
Skip to first unread message

Jonathan Sutcliffe

unread,
Feb 15, 2010, 6:06:52 AM2/15/10
to Django users
Hi,

I am new to Django and Python and have what must be a basic question.

I want to pass a URL of the following form, containing an email
address that I will later look up contact information from.

e.g. http://localhost:8000/contact/ronald.ninnis%40cdu.edu.au/

I don't care about whether this is a valid email string I simple want
to pass the parameter across to the view and do the look up here.

I notice the @ is already %40 which has been escaped by the Javascript
in the calling application.

In my urls.py I have the following regex.

urls.py

(r'^contact/(?P<email>\[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})/$',
'entitlements.sfdc.views.contactdetail'),

But no match is found. Could someone point out the error of my ways?

Many Thanks,
Jono

Shawn Milochik

unread,
Feb 15, 2010, 9:10:34 AM2/15/10
to django...@googlegroups.com
It sounds like this might be a good case for a GET (querystring).

As in:


Then use something like request.GET.get('email', '') to get the value.
That seems easier and cleaner than trying to have a URL pattern regex match every valid e-mail address.

Shawn

Karen Tracey

unread,
Feb 15, 2010, 11:26:29 AM2/15/10
to django...@googlegroups.com
On Mon, Feb 15, 2010 at 6:06 AM, Jonathan Sutcliffe <jonosu...@googlemail.com> wrote:

I want to pass a URL of the following form, containing an email
address that I will later look up contact information from.

e.g.  http://localhost:8000/contact/ronald.ninnis%40cdu.edu.au/

I don't care about whether this is a valid email string I simple want
to pass the parameter across to the view and do the look up here.

I notice the @ is already %40 which has been escaped by the Javascript
in the calling application.

In my urls.py I have the following regex.

urls.py

 (r'^contact/(?P<email>\[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})/$',
'entitlements.sfdc.views.contactdetail'),

But no match is found. Could someone point out the error of my ways?

If you do not care if it is a valid email string why are you attempting any kind of 'looks like an email' matching in the url pattern?

As for what, specifically, is wrong with that pattern -- the \ after <email> looks wrong, and the fact that you've specified all uppercase letters to match yet the sample URL you show has lowercase letters looks like a problem, for starters. But really I would hesitate to try to 'fix' that pattern, particularly if you really don't care if it is a valid email address at this point in the code. Just switch it to something that accepts anything between the two slashes in the url, and if you do care about checking validity later in processing, use some other pre-existing email validation code to check the value once you've got it in the view.

Karen
Reply all
Reply to author
Forward
0 new messages