Introduction
In this chapter, you will learn the advanced URL handling in Django.
Advanced URL Handling
Here are the steps,
- Firstly create a Django project and make the Django app. If you have no idea how you can build it then read previous chapters for making the Django project.
- Now you have successfully created a Django project. It’s time to use advanced URL handling in Django.
- Now open urls.py file in the 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 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.
- 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 used as ID=0. Here 0 is by the 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 used user-id != ‘0’ in if condition. So you want to ask that above id=0, it 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.
- 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 the below image.
- Now Enter again URL in browser as http://127.0.0.1:8000/user/0/
- Now again I will give you a 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 a 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 has published on the website you can use Below URL method syntax to 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.
Summary
In the next chapter, we will learn about the admin interface provided by Django.