{"id":9830,"date":"2014-10-10T05:03:55","date_gmt":"2014-10-09T18:03:55","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=9830"},"modified":"2014-10-10T05:03:55","modified_gmt":"2014-10-09T18:03:55","slug":"django-python-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/django-python-primer-tutorial\/","title":{"rendered":"Django Python Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/Django\/\"><img decoding=\"async\" style=\"float:left;border: 15px solid pink;\" alt=\"Django Python Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/Django\/Django_Python-14of.jpg\" title=\"Django Python Primer Tutorial\"  \/><\/a><p class=\"wp-caption-text\">Django Python Primer Tutorial<\/p><\/div>\n<p><a target=_blank title='Django home page' href='https:\/\/www.djangoproject.com\/'>Django<\/a> is a high-level Web framework that utilizes the Python programming language.  You may remember from some time back with <a target=_blank href='#pwpt' title='Python Webserver Primer Tutorial'>Python Webserver Primer Tutorial<\/a> as shown below, that we intimated we would try this out sometime.  Django helps you achieve rapid, clean, pragmatic design and development of web apps, with all those advantages Python offers.<\/p>\n<p>As one of the Three P&#8217;s you&#8217;d expect Python to be capable of this, and, indeed, it can be a complete replacement for PHP as your server-side language.  It is growing in popularity, and can see its appeal for the clear-thinkers amongst us.   Its data structures are appealing to many programmers, and with more and more Python code written, the more powerful it will become in the Open Source world.   So combine this with Django&#8217;s web app interfacing to databases (<a target=_blank href='http:\/\/www.sqlite.org\/' title='SQLite database'>SQLite<\/a> (database) is installed when you install Django, and you can interface to others, such as PostgreSQL or MySql) and you have a great environment to make powerful and interesting web apps.<\/p>\n<p><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/Django\/\" title='Click picture'>Today<\/a> we install Django to a pre-existant Python installation that sits happily alongside a local MAMP Apache\/PHP web server without concern.   If you do this yourself, too, please head for the latest version, because there have been recent security patch improvements to Django.  We follow the Django website&#8217;s <a target=_blank href='https:\/\/docs.djangoproject.com\/en\/1.7\/intro\/tutorial01\/' title='Django tutorial'>first tutorial<\/a> to do a small &#8220;poll&#8221; web app which we call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/Django\/django.zip\" title='Downloadable zip of resultant web app code'>&#8220;mysite&#8221;<\/a> as you would if <a target=_blank title=\"yer head's a marley\" href='http:\/\/www.slang.ie\/index.php?county=Derry&#038;entry=Yer+head%27s+a+marley'>&#8220;yer head&#8217;s a marley&#8221;<\/a> (as they used to say in <a target=_blank title='Coleraine, Northern Ireland' href='https:\/\/www.google.com.au\/search?q=%22Coleraine+map%22&#038;client=firefox-a&#038;hs=IvR&#038;rls=org.mozilla:en-US:official&#038;channel=sb&#038;tbm=isch&#038;imgil=sA7TQiVmMHp3UM%253A%253BlpeTkIR9YifiXM%253Bhttp%25253A%25252F%25252Fwww.colerainebc.gov.uk%25252Fshow.php%25253Fid%2525253D154&#038;source=iu&#038;pf=m&#038;fir=sA7TQiVmMHp3UM%253A%252ClpeTkIR9YifiXM%252C_&#038;usg=__mBCQ3-XoAbKcW-uYBCDAiCN8poQ%3D&#038;biw=1280&#038;bih=658&#038;ved=0CCoQyjc&#038;ei=zRclVKuMGJDMoASlxYHoCg#facrc=_&#038;imgdii=_&#038;imgrc=sA7TQiVmMHp3UM%253A%3BlpeTkIR9YifiXM%3Bhttp%253A%252F%252Fwww.colerainebc.gov.uk%252Fcontent_gfx%252Fother%252FCBC%252520Close%252520up%252520map.jpg%3Bhttp%253A%252F%252Fwww.colerainebc.gov.uk%252Fshow.php%253Fid%253D154%3B500%3B451'>Coleraine<\/a>).  Along the way you&#8217;ll see a Django http:\/\/localhost:8080 local web server we&#8217;ll add to the web server list in <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=4112' title='Cassini++ WebServer Primer Tutorial'>Cassini++ WebServer Primer Tutorial<\/a>.<\/p>\n<p>Lots of sidetracks here but hope you&#8217;ll bear with the mistakes, and if you are beginning, perhaps, sidestep those mistakes.<\/p>\n<hr \/>\n<p id='pwpt'>Previous relevant <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7056' title='Python Webserver Primer Tutorial'>Python Webserver Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/\"><img decoding=\"async\" id='pimqgmovh' onmouseover=\"var xqw=this.src;  if (xqw.indexOf('6') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/Python_WebServer-1of.jpg'; } else if (xqw.indexOf('1') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/Python_WebServer-2of.jpg'; }  else if (xqw.indexOf('2') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/Python_WebServer-3of.jpg'; } else if (xqw.indexOf('3') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/Python_WebServer-4of.jpg'; } else if (xqw.indexOf('4') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/Python_WebServer-5of.jpg'; } else if (xqw.indexOf('5') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/Python_WebServer-6of.jpg'; }  \"  style=\"float:left;border: 15px solid pink;\" alt=\"Python Webserver Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/Python_WebServer-6of.jpg\" title=\"Python Webserver Primer Tutorial (try twirling round bottom of image for a synopsis ... 'do the twirl now')\"  \/><\/a><p class=\"wp-caption-text\">Python Webserver Primer Tutorial (try twirling round bottom of image for a synopsis ... 'do the twirl now')<\/p><\/div>\n<p>Today&#8217;s tutorial shows yet another approach regarding the creation of a local Web Server.   Today we create a very simple Python Web Server, and give thanks to <i>Python and XML<\/i> by Christopher A. Jones and Fred E. Drake, for the excellent &#8220;basis&#8221; code on pp 198-200.   This tutorial could use <a target=_blank title='Go Webserver Primer Tutorial' href='#gwpt'>Go Webserver Primer Tutorial<\/a> as shown below, for compare and contrast purposes, and you may want to refer to our growing list of Web Server ideas at <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=4112' title='Cassini++ WebServer Primer Tutorial'>Cassini++ WebServer Primer Tutorial<\/a> and perhaps <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=3491' title='Web Server Primer Tutorial'>Web Server Primer Tutorial<\/a> and maybe <a target=_blank href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=4066' title=\"Apache WebServer on Mac and The Three P\u2019s Primer Tutorial\">Apache WebServer on Mac and The Three P\u2019s Primer Tutorial<\/a>.<\/p>\n<p>As one of the Three P&#8217;s you&#8217;d expect Python to be capable of this, and, indeed, it can be a complete replacement for PHP as your server-side language.   Believe it is growing in popularity, and can see its appeal for the clear-thinkers amongst us.   Its data structures are appealing to many programmers, and with more and more Python code written, the more powerful it will become in the Open Source world.<\/p>\n<p>So this Python&#8217;s Web Server &#8220;ingredients&#8221; (ie. methods) for class ourRequestHandler are:<\/p>\n<ul>\n<li>do_GET helps with GET requests<\/li>\n<li>do_POST helps with POST requests<\/li>\n<li>ourPrintBrowserHeaders prints HTTP headers<\/li>\n<li>ourPrintCustomHTTPResponse takes response and sends code and custom headers back to browser<\/li>\n<\/ul>\n<p>Link to downloadable Python programming source code for a rudimentary webserver could be called <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/PythonHTTPServer.py_GETME\" title=\"PythonHTTPServer.py\">PythonHTTPServer.py<\/a> (which oversees the &#8220;POST&#8221; test HTML <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Python\/WebServer\/ourTestFormPage.html_GETME\" title=\"ourTestFormPage.html\">ourTestFormPage.html<\/a>) as you wish.<\/p>\n<p>Are you looking for a commercial Python web framework?   You could try <a target=_blank title='Django' href='https:\/\/www.djangoproject.com\/'>Django<\/a>.<\/p>\n<p>Will leave you with a <a target=_blank title='Yes, it is true' href='http:\/\/www.youtube.com\/watch?v=G6D1YI-41ao'>chuckle<\/a>, m&#8217;lord.<\/p>\n<hr \/>\n<p id='gwpt'>Previous compare and contrast <a target=_blank title='Go Webserver Primer Tutorial' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=3969'>Go Webserver Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Go\/Webserver\/Go_Webserver_Primer.jpg\"><img decoding=\"async\"  style=\"border: 15px solid pink;\"  style=\"float:left; border: 15px solid pink;\" alt=\"Go Install Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Go\/Webserver\/Go_Webserver_Primer.jpg\" title=\"Go Webserver Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">Go Webserver Primer Tutorial<\/p><\/div>\n<p>Did you wake up wanting to write your own Web Server?   No?   Well, what are WE to do with YOU?   <em>&#8220;Lefty?   Mugsy?   Priscilla?   Let &#8217;em &#8216;av it?    Yes, the web server, you &#8216;eard what oi said!   Sorry, you&#8217;re having lunch?&#8221;<\/em><\/p>\n<p>&#8230; four hours later &#8230;<\/p>\n<p><em>&#8220;Mugsy, what&#8217;s that triangular piece of paper &#8230; never mind &#8230; have you got that web server?&#8221;<\/em><\/p>\n<p>(Metallic tinkering sound)<\/p>\n<p><em>&#8220;You do know what a web server is, don&#8217;t you?  What&#8217;s that you&#8217;ve got there on that silver thing behind your back?&#8221;<\/em><\/p>\n<p><strike>(Metallic tinkering sound)<\/strike> &#8230; replaced by (Metallic sounds that are tinkering)<\/p>\n<p><em>&#8220;Lefty, do you know what is behind Mugsy&#8217;s back &#8230; and why have you got one of those triangular pieces <strike>of pap<\/strike> &#8230; oh, never mind.   Is that a link you want me to click?&#8221;<\/em><\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p><a href='#showme' onclick=\" var xxx=document.getElementById('showme');  xxx.style.display='block'; var xxxd=document.getElementById('dshowme');  xxxd.style.display='block';\">V<\/a><\/p>\n<p><a href='#showme' style='color:pink;' onclick=\" var xxx=document.getElementById('showme');  xxx.style.display='block'; var xxxd=document.getElementById('dshowme');  xxxd.style.display='block';\">Welcome to Mugsy&#8217;s and Lefty&#8217;s personalized link<\/a><\/p>\n<div style=\"width: 380px; height: 260px; overflow: hidden\">\n<img id='showme' src='http:\/\/www.findaspider.org.au\/info\/images\/Recognizing nests.jpg' style='display:none; margin-left:-350px;' title='Thanks to http:\/\/www.findaspider.org.au\/ for the image' onclick=' window.open(\"http:\/\/www.findaspider.org.au\/\");'><\/img>\n<\/div>\n<p id='dshowme' style='display:none;'>Da da da da!   &#8230; or should that be &#8230; Do do, do do, do do, do do!<\/p>\n<p>Our tutorial today builds on previous <a target=_blank href=\"#gipt\">Go Install Primer Tutorial<\/a> as shown below.<\/p>\n<p>Link to downloadable Go programming source code for a rudimentary webserver (about animals) could be called <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Go\/Webserver\/webserver.go_GETME\">webserver.go<\/a> as you wish.<\/p>\n<p>Link to Go &#8220;spiritual home&#8221; at <a target=_blank title='Go at Google' href='http:\/\/code.google.com\/p\/go\/'>Google<\/a>.<\/p>\n<p>Link to really good Go tutorials at <a target=_blank title='Go tutorials at Google' href='http:\/\/tour.golang.org\/'>Google<\/a>.<\/p>\n<p>Link to Go 1.1 Google Open Source Blog is <a target=_blank title='Go Open Source Blog' href='http:\/\/google-opensource.blogspot.hu\/2013\/05\/go-11-is-released.html'>here<\/a>.<\/p>\n<p>Link to really good Go web information at  <a target=_blank title='Go web information at Google' href='http:\/\/golang.org\/doc\/articles\/wiki\/#tmp_3'>Google<\/a>.<\/p>\n<hr \/>\n<p id='gipt'>Previous <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Go\/\">Go Install Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Go\/\"><img decoding=\"async\" id='imgigmail' onmouseover=\"var xqw=this.src;  if (xqw.indexOf('5') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Go\/Go_Google_Primer-0of.jpg'; } else if (xqw.indexOf('4') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Go\/Go_Google_Primer-5of.jpg'; } else if (xqw.indexOf('3') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Go\/Go_Google_Primer-4of.jpg'; }    else if (xqw.indexOf('2') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Go\/Go_Google_Primer-3of.jpg'; } else if (xqw.indexOf('1') != -1) { this.src = 'http:\/\/www.rjmprogramming.com.au\/Go\/Go_Google_Primer-2of.jpg'; }  else { this.src = 'http:\/\/www.rjmprogramming.com.au\/Go\/Go_Google_Primer-1of.jpg'; } \"  style=\"border: 15px solid pink;\"  style=\"float:left; border: 15px solid pink;\" alt=\"Go Install Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Go\/Go_Google_Primer-3of.jpg\" title=\"Go Install Primer Tutorial (try twirling round bottom of image for a synopsis ... 'do the twirl now')\" \/><\/a><p class=\"wp-caption-text\">Go Install Primer Tutorial (try twirling round bottom of image for a synopsis ... 'do the twirl now')<\/p><\/div>\n<p>Go (by Google) gave me that feeling like you are just seeing the tip of the iceberg, again.   A friend kindly put me onto this as a programming language which is an alternative to OOP but not just a function call type of language.<\/p>\n<p>Here we just install it (on a Mac 64bit laptop with Mac OS X 10.7.5) and do the old &#8220;<a target=_blank title=\"Don't say I don't give you anything\" href='http:\/\/www.rjmprogramming.com.au\/Go\/hw.jpg'>Hello World<\/a>&#8220;, but we will explore more in tutorials to come, and in any case here we do 4 x &#8220;hello world&#8221; with 3 different programs.   Meanwhile, have a read from <a target=_blank title='Go (2009) information from Wikipedia' href='http:\/\/en.wikipedia.org\/wiki\/Go_%28programming_language%29'>Wikipedia<\/a> about it, below:<\/p>\n<blockquote><p>Go, otherwise known as Golang, is an open source, compiled, garbage-collected, concurrent system programming language. It was first designed and developed at Google Inc.[7] beginning in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson.[3]<\/p>\n<p>The language was officially announced in November 2009 and is now used in some of Google&#8217;s production systems.[8] Go&#8217;s &#8220;gc&#8221; compiler targets the Linux, Mac OS X, FreeBSD, OpenBSD, Plan 9, and Microsoft Windows operating systems and the i386, amd64, and ARM processor architectures.[9]<\/p><\/blockquote>\n<p>You can download go (2009) programming source codes and rename to <a target=_blank title='Hello World in go (2009)' href='http:\/\/www.rjmprogramming.com.au\/Go\/hello.go_GETME'>hello.go<\/a>,  <a target=_blank title='Hello World in go (2009)' href='http:\/\/www.rjmprogramming.com.au\/Go\/helloworld.go_GETME'>helloworld.go<\/a>,  <a target=_blank title='Hello World x 2 in go (2009)' href='http:\/\/www.rjmprogramming.com.au\/Go\/slice.go_GETME'>slice.go<\/a>.<\/p>\n<p>Link to Go &#8220;spiritual home&#8221; at <a target=_blank title='Go at Google' href='http:\/\/code.google.com\/p\/go\/'>Google<\/a>.<\/p>\n<p>Link to really good Go tutorials at <a target=_blank title='Go tutorials at Google' href='http:\/\/tour.golang.org\/'>Google<\/a>.<\/p>\n<p>Link to Go 1.1 Google Open Source Blog is <a target=_blank title='Go Open Source Blog' href='http:\/\/google-opensource.blogspot.hu\/2013\/05\/go-11-is-released.html'>here<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d3121' onclick='var dv=document.getElementById(\"d3121\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?cat=42\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d3121' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d3969' onclick='var dv=document.getElementById(\"d3969\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?cat=42\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d3969' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d7056' onclick='var dv=document.getElementById(\"d7056\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Python\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d7056' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr \/>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d9830' onclick='var dv=document.getElementById(\"d9830\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Cassini\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d9830' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Django is a high-level Web framework that utilizes the Python programming language. You may remember from some time back with Python Webserver Primer Tutorial as shown below, that we intimated we would try this out sometime. Django helps you achieve &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/django-python-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,12,37],"tags":[190,299,345,997,1012,1188,1267,1319,1401,1411],"class_list":["post-9830","post","type-post","status-publish","format-standard","hentry","category-database","category-elearning","category-tutorials","tag-cassini","tag-database-2","tag-django","tag-programming","tag-python","tag-sqlite","tag-the-three-ps","tag-tutorial","tag-web-app","tag-web-server"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/9830"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=9830"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/9830\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=9830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=9830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=9830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}