50π
In regular expressions, ^
and $
are special characters.
^
(Caret):
^
matches the start of the string.
Lets say my regex was ^a
, then the regex will look for a
in the start of the string:
'a' # Matches 'a' in 'a'
'abc' # Matches 'a' in 'abc'
'def' # Not match because 'a' was not at the beginning
$
(Dollar sign):
$
matches the end of the string.
If my regex was b$
, then it will match b
at the end of the string:
'b' # Matches 'b' in 'b'
'ab' # Matches 'b' in 'ab'
'abc' # Does not match
Using r'^$'
:
Using both ^
and $
together as ^$
will match an empty line/string.
url(r'^$', views.indexView, name='index')
When Django encounters an empty string, it will go to the index
page.
Using r''
:
When you use r''
, Django will look for an empty string anywhere in the URL, which is true for every URL.
So, if your urlpattern was like this:
url(r'', views.indexView, name='index')
All your urls will go to index
page.
11π
^$
means nothing is between the start and end β¦ this only matches the empty string
''
means an empty string(but does not specify anything about the beginning or end of the entire string) so when you encounter anything in the string it say well that matches 'asdasd'
for example has a matching empty string at the beginning⦠the remaining is passed to the new url rules script (in this case everything remains)
if instead your second rule was 'a'
then it would match the first a in the asdasd
and pass sdasd
to the new url matching rule-set
disclaimer that this is probably a gross oversimplification, but basically true
- [Django]-Extending Django Admin Templates β altering change list
- [Django]-HTML Forms without actions
- [Django]-Django: Check if settings variable is set
1π
^$ β it specifies the start and end points of a URL string.
β β β An empty string in URL method says, if any other URL pattern encountered that is not defined in the url pattern, then the corresponding empty string view should be called
- [Django]-Why are read-only form fields in Django a bad idea?
- [Django]-Trying to parse `request.body` from POST in Django
- [Django]-Django 1.4 β can't compare offset-naive and offset-aware datetimes
- [Django]-Django's ModelForm unique_together validation
- [Django]-Django, Retrieve IP location
- [Django]-How can i get the running server URL