Saturday, January 07, 2017

Overview MVA Django Web App Marathon


As per my estimate the Course was to be completed in  a day and was about to complete it , when in the final Module  while deploying the app  and database on the cloud it finally happend . Connecting an  application to the database with the right connector is always a lengthy task for the first time ...in finding the right database  ....setting up the connection and adding the right library to the project (that's always the part which troubles the  most if you are trying a latest framework to an old database)
whether it be Qt , C# , and now Django connecting Mysql Client Package to a Python Environment 3.5  took me almost 2 days to fix as 3.5 being the latest release wasn't  supported in SQL CLIENT

So After many GIT  revert commit and trying multiple  solution was able to fix it by both changing  the Environment 3.4   or by using the same 3.5 version and manually building and linking the package .

Link  : http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

Once Fixed Deploying and trying the App on both Azure and AWS  was a piece of Cake  ;)

Looking Forward to make few of Small  apps before starting with a project and learning JavaScript and Bootstrap before my College reopens  and continuing my Code practice at hacker-rank  side by side

Web APP  : http://databasefun20170107035036.azurewebsites.net/






Django –Marathon 6/6

Deploying and Interface




Django -Marathon 5/6

Layouts and Formats 


Template Example  


// Render need request object 
// Render_to_response does not need response request 


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

Django -Marathon 3/6

How to create a object in Django ?
  • -          import django
  • -          django.setup()
  • -          from app.models import Artist
  • -          newArtist=Artist(name =”Artist Name ”,year_formed=2015);
  • -          newArtist.save();

-          // Save Function is inherited in the model class
-          // check migrations carefully if save is not working  ‘


Updating an Object
  • -          newArtist.name="Maroon"
  • -          newArtist.save()



Queries in Django 


-         
>>> allArtist=Artist.objects.all() [.all() to get all records  ]
>>> allArtist[0].name
>>> allArtist[0].ID
>>> altArtist=Artist.objects.get(id=1) [Use try catch in get for exception]
>>> altArtis[0]
Using Basic Filter Statement in the “get” Statement 
>>> console=Artist.objects.get(name="Maroon")
>>> print (console.id)


Using filter on the objects

>>> console=Artist.objects.filter(name__startswith="M")
// ‘I’ for case insensitive search 
>>> console=Artist.objects.filter(name__istartswith="M")
// using exact match in the data
>>> console=Artist.objects.filter(name__exact="Maroon")
>>> console=Artist.objects.filter(name__iexact="Maroon")
// Displaying multiple Query results using for Loop 
>>> for artist in console:
...     print(artist.name)

// Using multiple filter restrictions conditions in the ORM query  ‘.’ 
Operator

>>> console=Artist.objects.filter(name__iexact="Maroon").filter(year_formed=2015)
>>> console=Artist.objects.filter(name__iexact="Maroon").exclude(year_formed=2015)

// using Foreign Key  for connecting two tables [Join Statements]


>>> Album(name="Up",artist=allArtist[0]).save()
>>> Album(name="down",artist=allArtist[0]).save()
>>> newAlbum=Album.objects.get.all()
Where allArtist is an instance OF THE CLASS Artist , Inserting foreign key elements by referring them to the instance of another class 
>>> for storage in newAlbum:

...     print(storage.id,storage.name)