0

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.

2
  • I would recommend to pass the url in a GET parameter rather than putting it in the url path. Commented Aug 20, 2016 at 18:57
  • Interesting. Can you elaborate why? Commented Aug 20, 2016 at 19:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't it be .+ as at-least something is required.
Yes.. Sorry I did not notice the + in question.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.