Re: URL RegEx problem for oembed

2008-07-25 Thread Fredrik Lundh

Marty Alchin wrote:

> Also, though, I have to ask why you're using such a complicated URL
> pattern in the first place? For some applications, I can understand
> accepting a full URL, but your pattern is expecting something very
> specific, which means you're not looking to accept just any arbitrary
> URL. Why bother using the full URL at all then? Couldn't you just
> append a portion of it instead, and infer the rest in your code? If
> you're after a full, arbitrary URL, your regex is far too specific at
> this point.

The OP's trying to implement http://oembed.com/, but missed that the
part that starts with a "?" is a URI query.  Such queries are parsed by 
Django and are passed to the views via special QueryDict attributes; see:

 http://www.djangoproject.com/documentation/request_response/

for details.




--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: URL RegEx problem for oembed

2008-07-25 Thread Marty Alchin

On Fri, Jul 25, 2008 at 11:31 AM, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
> Marty Alchin wrote:
>> Also, though, I have to ask why you're using such a complicated URL
>> pattern in the first place? For some applications, I can understand
>> accepting a full URL, but your pattern is expecting something very
>> specific, which means you're not looking to accept just any arbitrary
>> URL. Why bother using the full URL at all then? Couldn't you just
>> append a portion of it instead, and infer the rest in your code? If
>> you're after a full, arbitrary URL, your regex is far too specific at
>> this point.
>
> The OP's trying to implement http://oembed.com/

Ah. I had noticed the mention of oembed, but hadn't looked to see what
it was or how it worked, so I didn't realized it forced such a
convention. Not my cup of tea, but then, I don't need to implement it.
:) Thanks for the link.

-Gul

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: URL RegEx problem for oembed

2008-07-25 Thread Marty Alchin

On Fri, Jul 25, 2008 at 8:23 AM, timc3 <[EMAIL PROTECTED]> wrote:
> Just isn't working, and it looks like a RegEx problem to me, but I am
> not sure why..

It's usually best to explain what isn't working, exactly. Is it just
not being matched, and you're getting a 404? Is your urls.py giving
you errors? Is it matching but not filling the variables with the
right values? There's not a lot to go on here.

> I am trying to match the following URL:
>
> http://127.0.0.1:8000/services/oembed/?ref=http://127.0.0.1:8000/media/slug/guid/

First, you have a problem. URL patterns don't match query strings, so
Django will only pass "services/oembed/" into the pattern matching
logic. If you want the whole thing to be matched, so you can filter
out the specific pieces (which, below, it looks like you do), you'll
need to drop the "?ref=" and try to match something like this instead:

http://127.0.0.1:8000/services/oembed/http://127.0.0.1:8000/media/slug/guid/

> And the regex that I am using:
>
> urlpatterns = patterns('',
>url(r'^oembed/(?P[-\w]+)//(?[-\w]+)/media/(?P[-
> \w]+)/(?P[-\w]+)/$', 'brokersite.services.views.mediaoembed'),
> )

Wow. Okay, a few things:

* First, I'm getting a syntax error when I try to compile this regex
directly. You're missing a "P" after the question mark in your 
group.

* You're also missing a colon after the  group, so it's
expecting to match "http//" instead of "http://";.

* Your  group also doesn't need to be a full [-\w+]. If you're
truly only asking for HTTP addresses, it can be much more specific,
which will serve you better in the long run. Consider:
(?Phttps?). That'll match both "http" and "https" but not things
like "ftp", "aim", "gopher", etc. Also, dashes aren't allowed in the
protocol anyway, so that part can go away easily. Perhaps better yet,
you could go with "http(?Ps?)", so you'd get a "secure"
argument that you can easily use in things like "if secure:" later on.

* Your  group, on the other hand, is matching far too little
what you're after. For starters, \w doesn't match periods, so you'll
never match a valid domain or IP address that way. Also, since your
example includes a port number to match, you also need to check for
colons. Try something like this, (?P[-:\.\w]+), though I still
can't guarantee that'll match everything you might come up against.

Basically, it looks like you saw an example somewhere of using [-\w]+
to match slugs, and thought it was a useful general-purpose pattern to
use, which isn't the case. Regular expressions can be complicated
things if you want them to be, and you're certainly asking for a
complicated case. Each group needs to be written for the content it'll
actually match against, so you need to be prepared for it. Try doing
some reading on regular expressions elsewhere to learn some of these
finer details.

Also, though, I have to ask why you're using such a complicated URL
pattern in the first place? For some applications, I can understand
accepting a full URL, but your pattern is expecting something very
specific, which means you're not looking to accept just any arbitrary
URL. Why bother using the full URL at all then? Couldn't you just
append a portion of it instead, and infer the rest in your code? If
you're after a full, arbitrary URL, your regex is far too specific at
this point.

Anyway, I hope I helped at least a little bit.

-Gul

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---