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 urls.py 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'),  
    4.   
    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 views.py file and write code as below for defining views for above given URLS.

    code

  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 http://127.0.0.1:8000/user/345/

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

    browser
  10. Now Enter again URL in browser as http://127.0.0.1:8000/user/0/

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

  12. Again Open urls.py file and write code as below.

    code
  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 views.py for making view part of this URL

    code
  15. Now open browser and Navigate URL as http://127.0.0.1:8000/Alwar/search/Hotels

    browser
  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]+)/$', ‘main.views.blog’)

  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 urls.py file.

Up Next
    Ebook Download
    View all
    Learn
    View all