Python Django Tutorial: Advanced URL Handling - Part Four

Introduction

 
Today, I will take a 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 a Django project and make the Django app. If you have no idea how you can build it then read the above article for making the Django project.
     
  2. Now you have successfully created a Django project. It’s time to use advanced URL handling in Django.
     
  3. Now open urls.py file in the Django project folder and do work as below step for learning Django URL handling.
     
  4. Write below code as given:
    1. from django.conf.urls import url  
    2. urlpatterns = [    
    3.     url(r'^$''main.views.firstView', name='home'),    
    4.     url(r'^user/(?P<id>[0-9]+)/$''main.views.userid', name='userid'),      

    Here (?P<id>[0-9]+) is used for making regular expression URL. ?P is used for starting regular expression syntax with a 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 used 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 a string. So I want to tell you when any parameter is getting by URL that is navigated in a browser that always is in the 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 the 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 a 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 a 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 has published on the website you can use Below URL method syntax to do this.
    1. 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 at the end of this tutorial I want to say Django has easy URL handling rather than other frameworks of other languages. Django also makes filtering URLs for different -2 views using include method in urls.py file.