Saturday, January 07, 2017

Django –Marathon 4/6

Topic Covered: URL and Routing

 
 What is the need of routing  ? 

  • -          To make URL easy and better for SEO 
  • -          To not show user specific files like .html and .py as request
  • -          To show details as per user request or action 




 
Django uses Regular expressions for patterns (called perl regular expression)




  • $Contact  ---must start with contact ie contactus
  • Us^ --- end with us ie  contactus
  • \d – for digits 
  • \D – Non digit 
  • \w – Word character  or alphanumeric  (A-Z,0-9,_)
  • \W – non word character
To Create you own range use square bracket 

-          [a-z] lower case a to z
-          [az] specifically a or z
-          [a-zA-Z ] means lower case a through Z  and upper A through Z ie all Letters of alphabets


For Specific Choices  ,Few Examples  [Look more  ] // Regular Expressions 

  • -\d   For a single Digit
  • -\d\d od \d{2} for two digits , brackets applies to item immediately proceeding it  (asd\d){2}
  • -\d{1,2} means minimum and maximum number of items 
  • - \d+ : + means Zero or more 
  • -\d* : * means zero or more 
  • -\d? ? means item is optional






Reference For Lookup:

Django Chooses the First pattern that matches 









# Example  :

//in Url.py
url(r'^artists$',app.views.artists,name='home'),
url(r'^artists/(?P<name>[A-Za-z]+)$',app.views.artistdetails,name='artistdetails'),

//in views.py

from django.http import HttpRequest,HttpResponse

def artists(request):
    return HttpResponse('<html><head><title>Hello, Django!</title></head><body><h1>Helllo World</h1></body></html>');

def artistdetails(request,name):
    output='<html><head><title>'+name
    output+='</title></head><body><h1>'+ name
    output+='</h1></body></html>'
    return HttpResponse(output);


Start Server for output to see instantenous changes 

## !! Correction ..^ to Start $ to end

No comments:

Post a Comment