Django Urls

Let's open up the mysite/urls.py file in your code editor of choice and see what it looks like:

"""mysite URL Configuration

[...]
"""
from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

The admin URL, which you visited in previous chapter is already here:

    url(r'^admin/', admin.site.urls),

It means that for every URL that starts with admin/ Django will find a corresponding view(admin panel default by django).

Regular Expresions:
^post/(\d+)/$

    ^post/ is telling Django to take anything that has post/ at the beginning of the url (right after ^)

    (\d+) means that there will be a number (one or more digits) and that we want the number captured and extracted

    / tells django that another / character should follow

    $ then indicates the end of the URL meaning that only strings ending with the / will match this pattern


^ for beginning of the text

$ for end of text

\d for a digit

+ to indicate that the previous item should be repeated at least once

() to capture part of the pattern



Symbol  Matches

. (dot)     Any single character

\d          Any single digit

[A-Z]       Any character between A and Z (uppercase)

[a-z]       Any character between a and z (lowercase)

[A-Za-z]    Any character between a and z (case-insensitive)

+           One or more of the previous expression (e.g., \d+ matches one or more digits)

[^/]+       One or more characters until (and not including) a forward slash

?           Zero or one of the previous expression (e.g., \d? matches zero or one digits)

*           Zero or more of the previous expression (e.g., \d* matches zero, one or more than one digit)

{1,3}       Between one and three (inclusive) of the previous expression (e.g., \d{1,3} matches one, two or three digits)



([0-9]{4} 4digits only


([0-9]{2}) two digits only

'^$/'  empty string

 url(r'^$',

This regular expression will match ^ (a beginning) followed by $ (an end) - so only an empty string will match


0 comments:

Copyright © 2013 SoftKul