Monday, 12 July 2021

*Episode 6* DJANGO ( How to URL Mapping in Django)


 URL Mapping


     Our aim is to access a view via a URL. Django provides us with a mechanism named “URL Mapping”, which can be implemented by modifying the project file “url.py”. The file looks like this:


from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

upatterns = patterns(”,

#Examples

#url(r’^$’, ‘project1.view.home’, name = ‘home’),

#url(r’^blog/’, include(‘blog.urls’)),

url(r’^admin’, include(admin.site.urls)),

)


     A mapping is just a tuple in the URL patterns and is as follows:


from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

upatterns = patterns(”,

#Examples

#url(r’^$’, ‘project1.view.home’, name = ‘home’),

#url(r’^blog/’, include(‘blog.urls’)),

url(r’^admin’, include(admin.site.urls)),

url(r’^hello/’, ‘myapplication.views.hello’, name = ‘hello’),

)


    As you have noticed from the above code, there are three components associated in mapping. These are the following:


 1. ) The pattern- This is a regexp that matches the URL that you are in need of resoling and mapping.

 2. ) Python path to your view- This is the same as when one is importing a module.

 3. ) The name- For URL reversing to be done, one has to use the named URL patterns.

     This is what has been done in the above examples.


  •  Organization of URLs :-

     You need to create the file “url.py” for each application that you create to be able to organize your URLs effectively. The following code can be used for creation of such a file for the app “myapplication”:


from django.conf.urls import patterns, include, url

upatterns = patterns(”, url(r’^hello/’, ‘myapplication.views.hello’, name = ‘hello’),)


     The project “project1/url.py” should then change to the following:


from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

upatterns = patterns(”,

#Examples

#url(r’^$’, ‘project1.view.home’, name = ‘home’),

#url(r’^blog/’, include(‘blog.urls’)),

url(r’^admin’, include(admin.site.urls)),

url(r’^myapplication/’, include(myapplication.urls)),

)


     All of the URLs from the application “myapplication” have been included.

     To map a new view to the “myapplication/url.py”, the code for “myapplication/url.py” can be changed to the following:


from django.conf.urls import patterns, include, url

upatterns = patterns(”,

url(r’^hello/’, ‘myapplication.views.hello’, name = ‘hello’),

url(r’^morning/’, ‘myapplication.views.morning’, name = ‘morning’),

)


     Note that according to the above code, the new view is located in the “myapplication/morning”. The code given below can be used for refactoring the above:


from django.conf.urls import patterns, include, url

upatterns = patterns(‘myapplication.views’,

url(r’^hello/’, ‘hello’, name = ‘hello’),

url(r’^morning/’, ‘morning’, name = ‘morning’),)



  •  How to send Parameters to the Views :-

     For us to pass parameters, we just have to capture them using the regexp in our URL pattern. Consider the example view given below:


from django.shortcuts import render

from django.http import HttpResponse

def hello(request):

return render(request, “hello.html”, {})

def vwArticle(request, artId):

text = “Displaying the Number of Article : %s”%artId

return HttpResponse(text)


      Our aim is to map to the “myapplication/url.py” and we will be in a position to access it via the “myapplication/url.py”. The following code has to be implemented in the file “myapplication/url.py”:


from django.conf.urls import patterns, include, url

upatterns = patterns(‘myapplication.views’,

url(r’^hello/’, ‘hello’, name = ‘hello’),

url(r’^morning/’, ‘morning’, name = ‘morning’),

url(r’^article/(\d+)/’, ‘vwArticle’, name = ‘article’),)


     Please note that the order of your parameters is very important. Suppose that we are in need of a list of articles from a particular month of the year. The view can be changed to the following:


from django.shortcuts import render

from django.http import HttpResponse

def hello(request):

return render(request, “hello.html”, {})

def vwArticle(request, artId):

text = “Displaying the Number of Article : %s”%artId

return HttpResponse(text)

def vwArticle(request, month, yr):

text = “Displaying the articles of : %s/%s”%(yr, month)

return HttpResponse(text)


      The corresponding file “url.py” should be as follows:


from django.conf.urls import patterns, include, url

upatterns = patterns(‘myapplication.views’,

url(r’^hello/’, ‘hello’, name = ‘hello’),

url(r’^morning/’, ‘morning’, name = ‘morning’),

url(r’^article/(\d+)/’, ‘vwArticle’, name = ‘article’),

url(r’^articles/(\d{2})/(\d{4})’, ‘vwArticles’, name = ‘articles’),)


     You can then test the app and reverse your parameters and observe what happens. In my case, I got the following result:


Displaying the articles of : 2016/11


     Upon reversing the parameters, I got a different result. However, this is not what we need. To prevent this, we only have to link the URL parameter to our view parameter. Because of that, the file “url.py” will be as follows:


from django.conf.urls import patterns, include, url

upatterns = patterns(‘myapplication.views’,

url(r’^hello/’, ‘hello’, name = ‘hello’),

url(r’^morning/’, ‘morning’, name = ‘morning’),

url(r’^article/(\d+)/’, ‘vwArticle’, name = ‘article’),

url(r’^articles/(?P\d{2})/(?P\d{4})’, ‘vwArticles’, name = ‘articles’),)


    That’s it!


👈Episode 5(D).                                                                                Episode 7(D)👉

Share This Post

PRINT THIS POST

No comments:

Post a Comment

If you have any doubts. Please let me know.

Featured post

*Episode 1* MCQ for Govt. Job/ Private Job/ MNCs

  Topic:- One Word Substitution 1) Especially skilled in storytelling  Answer:- Raconteur 2) Fear of loneliness Answer:- Eremophobia  3) Usa...