darkhorse
darkhorse

Reputation: 8782

Using a URL as a URL parameter in Django

In my models, I have the following:

class Content(models.Model):
    address = models.URLField(unique=True)

In my urls, I have this pattern:

url(r'^content_detail/(?P<address>[a-zA-Z-_./:0-9-_+=?;~@#%^&*(){}|`<>]+)/$', views.content_detail),

Yes, seriously, thats the pattern Im using. Basically, i took a look at all the characters that can be used in a URL, and made the pattern. Now, my question is, will it work? Its been working fine in my testing but I am no regex expert. Any way to improve this? If possible, can anyone give me a django url specific regex for URLs? Thanks a lot.

Upvotes: 0

Views: 93

Answers (2)

rafalmp
rafalmp

Reputation: 4068

Your regex will fail on IDNs (Internationalized Domain Names) containing Unicode characters. I think the easiest way would be matching on .+ as Aswin Kumar K P wrote in his answer, then validating the captured pattern using Django's URLValidator. You can find Django's regular expressions for URLs in that validator's source code: URLValidator

Upvotes: 1

Aswin Kumar K P
Aswin Kumar K P

Reputation: 1102

I think you have added all the possible symbols, alphabets and numbers in your URL.

url(r'^content_detail/(?P<address>.*)/$', views.content_detail),

This allows all characters in your URL.

Upvotes: 1

Related Questions