Python Django Tutorial: Advanced URL Handling - Part Four

Today, I will take o na new part of Python Django from my tutorial series. In this part you will learn the advanced URL handling in django.

I already told you the following:

Here are the steps,
  1. Firstly create django project and make django app. If you have no idea how you can build it then read above article for making django project.

  2. Now you have successfully created django project. It’s time to use advanced URL handling in Djnago.

  3. Now open file in django project folder and do work as below step for learning django URL handling.

  4. Write below code as given:

    from django.conf.urls import url,
    1. urlpatterns = [  
    2. url(r'^$''main.views.firstView', name='home'),  
    3. url(r'^user/(?P<id>[0-9]+)/$''main.views.userid', name='userid'),  
    5. ]  
    Here (?P<id>[0-9]+) is used for making regular expression URL. ?P is used for starting regular expression syntax with variable value. ID denotes the value of ID will be in between [0-9] and + symbol is used these integer values can repeat many times.

  5. Now open file and write code as below for defining views for above given URLS.


  6. In above code image method takes two parameters first is always used for requests to handle view and second is uses as ID=0. Here 0 is by default value of ID if not provided. But this can’t work now because it doesn’t have any way to use by default value by this method.

  7. One thing is most important here that is I user id != ‘0’ in if condition.So you want to ask that above id=0 denotes id is an integer and in below code id is working as string.So I want to tell you when any parameter is getting by URL that is navigated in browser that always is in form of string it is not dependant on what type of variable I have defined.

  8. After this Open browser and navigate URL as

  9. Now you will see in browser output as shown in below image.

  10. Now Enter again URL in browser as

  11. Now again I will give you different URL example.

  12. Again Open file and write code as below.

  13. In above code of URL method (?P<City>[a-zA-Z]+)/search/?(?P<Query>[0-9a-zA-Z]+) denotes City variable that has [a-zA-Z] characters with many times after search and then use Query variable that wants user to search.

  14. Now open for making view part of this URL

  15. Now open browser and Navigate URL as

  16. Space is not working if you search as Lowest price Hotels then Query variable only gets Lowest word for creating search. If you want to make space then some regular expression techniques you have used in django.

  17. If you want to make a blog that contains Date in URL for showing particular post time that have published on website you can use Below URL method syntax for do this.

    url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', ‘’)

  18. Above regular expression makes URL as /articles/2016/05/101

  19. Here 2016 denotes year and 05 denotes month and 101 denotes the post id.

So in the end of this tutorial I want to say django has easy URL handling rather than other framework of other languages. Django also makes filtering URL for different -2 view using include method in file.


Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now