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) 

Django-Marathon 2/6

Django Vs ___________ ?
  • MVC: Asp.net MVC 
  • Django create forms in predefined form layout and need tweak to modify
  • Routing is done by controller in asp.net in Django do using expression 




Create Project using Django-Admin :

>>>django-admin startproject app-name folder-name
>>>django-admin startapp app theapp // theapp is name of folder
\\Add  app created in  settings.py

What  is Model  : MVC controller Diagram ?
  •                                 Data : regarding data as per projects 
  •                                 Model  : Class  for project , prevent sql injection attacks if using models

ORM:. Object relational mapping 
  • -          Converts queries into SQL 
  • -          Converts results into objects 




Implementation of ORM
  • -          Hibernate in Java
  • -          Entity Framework in LINQ 
  • -          Django

>> Save , Delete & Update queries are managed by Django Framework contained in (models.Model)
>> Properties: size,datatype,Nullability 

-          Syntax property_name=models.Type(parameters)

-           Eg name=models.IntegerField(null/default/blank/max_length)


Primary Key is added automatically else : 

Foreign Key  Syntax

                Artist=models.ForiegnKey(Artist) // Automatically map columns to  object 

  • Use (name_name instead of nameNAme ) for models to help out  in Autogenerated forms




What database Django Support  ?
  • -          SQLite (default in settings.py)
  • -          MySQL
  • -          Microsoft SQL Server

How to create Database  ?
  • -          Makemigrations : create package and send to database 
  • -          Sqlmigrate : display sql statement done by migrate
  • -          Migrate: take the package and create database 

Command for Migration 
-          python manage.py makemigration  --name migration_name  app
-          python manage.py showmigrations app  [to show all migrations done yet  ]
-          python manage.py sqlmigrate app  0001_initial  [to sql command generated ]
-          python manage.py migrate

-          python manage.py app  migrate app 0001_initial  

Django-Marathon 1/6


What is Django ?  [as per Djangoprojects.com]
·         Django makes it easier to build web apps more quickly and with less code 
·         The web frame work for perfectionist with deadlines

Features of Django 
  • ·         Tempting and Layouts 
  • ·         Object relational mapper (ORM )
  • ·         Site administration
  • ·         Security 
  • ·         Easy to understand and really powerful 


Model, View, Controller Pattern   (MVC )- Gang of four






// Bootstrap for designing the Front of the application 

Installing required tools from PTVS team  :  https://github.com/Microsoft/PTVS/releases/v2.2
Downloaded PTVs Sample   : ptvs.blob.core.windows.net/download/PTVS%20Samples%202.2.vsix

Quick Intro About Files in Django Application :
  • Manage.py  : Manage our database
  • Settings.py : Location of database  part of ORM 
  • url.py : reroute url
  • views.py  : in Django terminology combine data with templates for output
  • models.py :  for models 

Tuesday, January 03, 2017

Django - Marathon 6 Hours (Start)

After completing django from Code-academy and practicing many Starter Competitive programming questions to  brush up my python  from Hacker Rank and Code-chef  , Django has been for long on my To-do list  so  yesterday  after bit googling and finally locking down on Django Framework (skipping Flask ) to work on I installed the Django 1.10.3 using the pip installer  and found this Learning Module at MVA   -" Developing Website with Python and Django " .

Taking the advantage of my ongoing college vacation ...My Tonight's Target is completing the entire Module from MVA and Keep Posting Notes for Quick Catch-up Later on:  


Links  

Long Time

Long Time Since i blogged  about something and