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,
- 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.
- Now you have successfully created django project. It’s time to use advanced URL handling in Djnago.
- Now open urls.py file in django project folder and do work as below step for learning django URL handling.
- Write below code as given:
from django.conf.urls import url,
- urlpatterns = [
- url(r'^$', 'main.views.firstView', name='home'),
- 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 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.
- Now open views.py file and write code as below for defining views for above given URLS.
- 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.
- 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.
- After this Open browser and navigate URL as http://127.0.0.1:8000/user/345/
- Now you will see in browser output as shown in below image.
- Now Enter again URL in browser as http://127.0.0.1:8000/user/0/
- Now again I will give you different URL example.
- Again Open urls.py file and write code as below.
- 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.
- Now open views.py for making view part of this URL
- Now open browser and Navigate URL as http://127.0.0.1:8000/Alwar/search/Hotels
- 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.
- 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’)
- Above regular expression makes URL as /articles/2016/05/101
- 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.