Introduce Django


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Django follows Python's core concept of keeping things as simple as possible and understandable. This leads to much more maintainable code in the future. Libraries play a huge part in choosing Django over Rails. Python has a library for just about everything Basically coding practices which help you put together some quick, clever code. This is nice, but much like Perl code it can end up making code a PITA to maintain. I cannot express how much better this is than than anything Ruby has. Ruby's module system doesn't even come close.
  • http://
  • Introduce Django

    1. 1. Introduce Django The Python Web Framework [email_address]
    2. 2. Web “ Framework ” ? <ul><li>A web application framework is a set of software tools and libraries to make it easier to create web applications. They typically provide functionality such as database access, templating and session management. </li></ul><ul><li>-- Wikipedia </li></ul>
    3. 3. Please Teach me Web Frameworks for Python! <ul><li>Guido van Rossum , Jan 27, 2006 </li></ul><ul><li>“”” After years of resistance, I'm finally finding myself building a web application again. I think the last time I did that was the &quot;faq wizard&quot; which still lives in Python's Tools directory. It was a CGI script …….””” </li></ul>
    4. 4. Which Part of &quot;No XML&quot; Don't You Understand? <ul><li>Guido van Rossum, January 31, 2006 </li></ul><ul><li>The XML crowd don't seem to get it. I don't *want* to use XML in my templates. Now can they leave me alone and stop telling me that I want XML? </li></ul><ul><li>I'm not saying that there's no use for XML. But if you see XML as the solution to every problem, well, that's a problem. </li></ul>
    5. 5. Django <ul><li>Oh My God It's Django! </li></ul><ul><li>August 17th, 2006 </li></ul><ul><li>Guido just pronounced: Django is the web framework </li></ul><ul><li>Won't be part of the core, but will be as 「 standard 」 as PIL or NumPy </li></ul><ul><li>He hopes that Django and TurboGears will converge </li></ul>
    6. 6. Popular Web Framework Today (via lang) <ul><li>Java: , Spring , WebWork, Tapestry, JSF </li></ul><ul><li>PHP:       , CakePHP , Code Igniter, Zend Framework </li></ul><ul><li>Perl: , Maypole </li></ul><ul><li>Python: ,TurboGears ,Pylons </li></ul><ul><li>Ruby: , Nitro </li></ul>
    7. 7. Chapter 1: The buzz words Why Django over other web frameworks.
    8. 8. Why Django? <ul><li> </li></ul><ul><li>Person 1: &quot;What do you do for a living?&quot; </li></ul><ul><li>Person 2: &quot;I work with computers.&quot; </li></ul><ul><li>Person 1: &quot;So do I! What do you do with computers?&quot; </li></ul><ul><li>Person 2: &quot;I'm a Web developer.&quot; </li></ul><ul><li>Person 1: &quot;So am I! Design, client-side programming or server-side programming?&quot; </li></ul><ul><li>Person 2: &quot;Server-side programming.&quot; </li></ul>
    9. 9. Why Django? [cont.] <ul><li>Person 1: &quot;Same here! Do you use dynamically typed languages or statically typed languages?&quot; </li></ul><ul><li>Person 2: &quot;Dynamically typed languages.&quot; </li></ul><ul><li>Person 1: &quot;So do I! Do you use a Web framework, or do you roll things on your own?&quot; </li></ul><ul><li>Person 2: &quot;I use a Web framework.&quot; </li></ul>
    10. 10. Why Django? [cont.] <ul><li>Person 1: &quot;So do I! Django or Rails? &quot; </li></ul><ul><li>Person 2: &quot;Django.&quot; </li></ul><ul><li>Person 1: &quot;Die, heretic scum! “ </li></ul><ul><li> ( 去死吧 異教徒雜碎 ) </li></ul>
    11. 11. Warning… <ul><li>One thing is clear, though: everyone's getting stuff done, regardless of their language choice. The whole debate isn't actually about productivity at all, even though most people still think it is. It's about whether you can stand to live in a house where the bed isn't made. </li></ul><ul><ul><li>-- Steve Yegge </li></ul></ul>
    12. 12. Dr. Larson's renowned treatise on non-inferring static type systems
    13. 13. Why Django #1 : Python <ul><li> </li></ul><ul><li>Simple as possible </li></ul><ul><li>Libraries. </li></ul><ul><li>Ruby programming tends to favor 'magic'. </li></ul><ul><li>Python's package system makes organizing your code so much easier. </li></ul>
    14. 14. Why Django #2: Documentation <ul><li>Every new Rails developer is told to pickup a copy of Agile Web Development for Rails . </li></ul><ul><li>The Django Book , which is currently being developed with the help of the community, and will be available online for free. </li></ul>
    15. 15. Why Django #3: Templating <ul><li>The Author initially really disliked the Django templating system. (same to me!) </li></ul><ul><li>Couldn't jam application code into the template. It truly restricted to only formatting content. </li></ul><ul><li>if you couldn't express something with the template system, you was doing something wrong. </li></ul>
    16. 16. Other reasons … (django over rails) <ul><li>#4: Speed </li></ul><ul><li>#5: Admin interface </li></ul><ul><li>#6: Generic views </li></ul><ul><li>#7: Simplicity and flexibility </li></ul><ul><li>#8: Reality, not buzz-words </li></ul><ul><li>Comment from me: However, Rails is indeed a great web framework if you like know ruby (a lot). </li></ul>
    17. 17. Web Framework Performance Compare (at rails wiki) <ul><li> </li></ul><ul><ul><li>Summary </li></ul></ul><ul><ul><li>Rails performed much better than Symfony. And Django performed much better than Rails. </li></ul></ul>
    18. 18. Comment at Catalyst User ‘ s blog <ul><li> </li></ul><ul><li>“”” Today I began working on a new project and decided to benchmark Catalyst and Rails for fun. See how my new favorable framework does against Rails. </li></ul><ul><li>Conclusion: It seems like Rails is roughly 62% faster than Catalyst at this time. … . (Django) Amazing and the winner ! I have the least experience with django for your information, mst Please don't kill me ”” </li></ul>
    19. 19. Django Design philosophies <ul><li>Loose coupling </li></ul><ul><li>Quick development </li></ul><ul><li>Don't repeat yourself (DRY) </li></ul><ul><li>Explicit is better than implicit </li></ul><ul><li>Consistency / Less code </li></ul><ul><li> </li></ul>
    20. 20. Chapter 2: Django Overview Django Web Framework 101
    21. 22. MVC,MTV – if it walks like a duck … <ul><li>MVC: Model-View-Controller Architecture </li></ul><ul><li>… it ’ s a duck </li></ul>MVC RoR Django Model model model View view template Controller controller view
    22. 23. Web Server Browser/Client URL Dispatcher: URLconf => View ( controller ) : Model : Database (mysql, postgresql, sqlite) Template HTML/XHTML CSS/Image Javascript Admin Generic View Request Response
    23. 24. How Django place files <ul><li>Database Wrapper  </li></ul><ul><li>Admin Framework  add class Admin declare in </li></ul><ul><li>Controller  </li></ul><ul><li>URL dispatcher  </li></ul><ul><li>Template System index.html </li></ul><ul><li> are just helper. </li></ul>
    24. 25. Template: Client-side Logic such as date format 18/12/2006&quot; or &quot;2006-12-18&quot; presentation need to solved without breaking the separation strategy HTML/XHTML CSS Javascript Structure : What Does it MEAN? Presentation :What does it LOOK like? Behavior : What Does it Do ? View(Controller): Server-side Logic Content: Django is for backend. You still have to know XHTML/CSS/Javascript for your front-end needs. The View in MVC isn’t easy as you might think
    25. 26. Still something great … <ul><li>Cache system </li></ul><ul><li>Internationalization </li></ul><ul><li>Middleware (Add-ons) </li></ul><ul><li>Sessions / Cookies </li></ul><ul><li>New Form (in svn / not in 0.95 Release) </li></ul>
    26. 27. Chapter 3: Hello, Django write a web announce system
    27. 28. I’ll show you…(the core idea) <ul><li>Object-relational mapper (ORM) </li></ul><ul><li>Automatic admin interface </li></ul><ul><li>Elegant URL design </li></ul><ul><li>Designer-friendly Template system </li></ul><ul><li>With 8 step (9 pages of slides) ! </li></ul>
    28. 29. Follow me. #1: Start project <ul><li>apt-get install django (or something like that) </li></ul><ul><li> startproject superhard </li></ul><ul><li>superhard/ </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
    29. 30. Follow me #2: Create application <ul><li>cd superhard </li></ul><ul><li>python startapp announce </li></ul><ul><li>superhard/announce/ </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul>
    30. 31. Follow me #3: Create Database <ul><li>Edit </li></ul><ul><li>DATABASE_ENGINE : sqlite3 </li></ul><ul><li>DATABASE_NAME :msg.db </li></ul><ul><li>apt-get install pysqlite2 sqlite3 </li></ul><ul><li>python syncdb </li></ul><ul><li># also create a superuser </li></ul>
    31. 32. Follow me #4: build your model <ul><li>superhard/announce/ </li></ul><ul><li>from django.db import models #auto generate </li></ul><ul><li>class Msg(models.Model): </li></ul><ul><li>name = models.CharField(maxlength=64) </li></ul><ul><li>date = models.DateTimeField('postdate') </li></ul><ul><li>msgtxt = models.TextField(maxlength=8192) </li></ul><ul><li>class Admin: </li></ul><ul><li>pass </li></ul>
    32. 33. Follow me #5: Write Your Template <ul><li>Edit TEMPLATE_DIRS in your </li></ul><ul><li>Don ’ t put template in your www directory. </li></ul><ul><li>superhard-template/announce_list.html </li></ul><ul><ul><li><html><head><title>test</title></head><body> </li></ul></ul><ul><ul><li>#(insert template code here) </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><li> </html> </li></ul>
    33. 34. Follow me #5: Write Your Template (cont.) <ul><li>{%if msgs_list %} </li></ul><ul><li>{% for msg in msgs_list %} </li></ul><ul><li><p> {{ }} at {{}} </p> </li></ul><ul><li><p> {{msg.msgtxt}} </p> </li></ul><ul><li>{% endfor %} </li></ul><ul><li>{%else %} </li></ul><ul><li><p>No Announce are available now.</p> </li></ul><ul><li>{%endif %} </li></ul>
    34. 35. Follow me #6: Write Your View <ul><li>superhard/announce/ : </li></ul><ul><li>from django.shortcuts import render_to_response </li></ul><ul><li>from superhard.announce.models import Msg </li></ul><ul><li>def announce_index(request): </li></ul><ul><li>latest_msg = Msg.objects.all().order_by('date')[:5] </li></ul><ul><li>return render_to_response('announce.html', {'msgs_list': latest_msg}) </li></ul>
    35. 36. Follow me #7: <ul><li>urlpatterns = patterns( </li></ul><ul><li>' superhard.announce.views ', </li></ul><ul><li>(r ' ^$ ' , ' announce_index ' ) , </li></ul><ul><li>('abc', 'announce_index'), </li></ul><ul><li># (r'^admin/', include('django.contrib.admin.urls')), </li></ul><ul><li>) </li></ul><ul><li>Uncomment urls for admin </li></ul><ul><li>django just need an urlpatterns object </li></ul>
    36. 37. #8: Let ’ s start play! <ul><li>python runserver </li></ul><ul><li>Add 'django.contrib.admin', 'superhard.announce', to your INSTALLED_APPS </li></ul><ul><li>python syncdb </li></ul><ul><li> </li></ul><ul><li> </li></ul>
    37. 38. Deploy <ul><li>Fastcgi or mod_python </li></ul><ul><li>Static content should be separate </li></ul><ul><li>Deploy on web hosting server not as easy as PHP. (for now) </li></ul><ul><li>But if it works, it works well ! </li></ul><ul><li>Lighttpd with fastcgi should be good. </li></ul>
    38. 39. Chapter 4 Generic View
    39. 40. Don’t Repeat Yourself (DRY) <ul><li>Generic views let you quickly provide common views of an object without actually needing to write any Python code </li></ul><ul><li>You don’t really have to write Python code. </li></ul><ul><li>It’s not MAGIC! Django provide most common patterns and you can extend it. </li></ul><ul><li>It’s nothing about View in MVC, it’s about reuse controller logic. </li></ul>
    40. 41. Reuse your logic <ul><li>&quot;Simple&quot; generic views </li></ul><ul><li>Date-based generic views </li></ul><ul><li>List/detail generic views </li></ul><ul><li>pagination </li></ul><ul><li>Create/update/delete generic views </li></ul><ul><li>You could use it in or </li></ul><ul><li>Simple logic => just use it in! </li></ul>
    41. 42. – get rids of <ul><li>from django.conf.urls.defaults import * </li></ul><ul><li>from superhard2.announce.models import Msg </li></ul><ul><li>urlpatterns = patterns( </li></ul><ul><li>'django.views.generic.list_detail', </li></ul><ul><li>(r'^$', 'object_list', </li></ul><ul><li>{ 'queryset':Msg.objects.all().order_by('date')[:5], </li></ul><ul><li>'template_name':'announce_list.html', #can omit </li></ul><ul><li>'template_object_name':'msgs', }), </li></ul><ul><li>(r'^admin/',include('django.contrib.admin.urls')), </li></ul><ul><li>) </li></ul>
    42. 43. Chapter 5 Real World Code Snippet
    43. 44. Welcome to Real World! <ul><li>Real world is messy. </li></ul><ul><li>Django is easy, but make a good web application is not. </li></ul><ul><li>You’ll glad you choose python. </li></ul><ul><li>Django is not perfect, but it’s good enough. </li></ul><ul><li>Don’t Repeat Yourself (DRY) </li></ul>
    44. 45. 1. Table relation. <ul><li>class Stb(models.Model): </li></ul><ul><li>stbSerial = models.CharField(primary_key=True) </li></ul><ul><li>stbSubGroupNum = models.ForeignKey('Subgroup') </li></ul><ul><li>class Subgroup(models.Model): </li></ul><ul><li>subGroupNum = models.CharField(primary_key=True) </li></ul><ul><li> pGroup = models.ForeignKey('Pgroup‘) </li></ul><ul><li>class Pgroup(models.Model) </li></ul><ul><ul><li>groupNum = models.CharField(primary_key=True) </li></ul></ul>
    45. 46. 2. Ajax via simplejson <ul><li>from django.utils import simplejson </li></ul><ul><li>def ajax_view(request): </li></ul><ul><li> jsondict = {“a&quot;:request.session[‘a’],“b&quot;:”hi”} </li></ul><ul><li> return HttpResponse(simplejson.dumps(jsondict)) </li></ul>
    46. 47. 3. Extend template tags. <ul><li>from django import template </li></ul><ul><li>register = template.Library() </li></ul><ul><li>@register.simple_tag </li></ul><ul><li>def tick_to_time(ticks): </li></ul><ul><li>import time </li></ul><ul><li>try: </li></ul><ul><li>return time.asctime(time.localtime(float(ticks))) </li></ul><ul><li>except: </li></ul><ul><li>return ““ #empty string </li></ul>
    47. 48. 4. Write your own decorator <ul><li>def login_required(f): </li></ul><ul><li>def wrap(request, *args, **kwargs): </li></ul><ul><li> if not request.session or ‘name’ not in request.session.keys(): </li></ul><ul><li>return HttpResponseRedirect(“/login”) </li></ul><ul><li> return f(request, *args, **kwargs) </li></ul><ul><li>return wrap </li></ul><ul><li>@login_required </li></ul><ul><li>def some_viewfunc_needlogin(request): </li></ul><ul><li>…… </li></ul>
    48. 49. 5. Use Syslog debug production server <ul><li> import syslog </li></ul><ul><li> error = “what ever you wanna log” </li></ul><ul><li>syslog.syslog(syslog.LOG_NOTICE,error) </li></ul>
    49. 50. Chapter 6 It’s not end…
    50. 51. Something you still should learn. (except Python) <ul><li>Django won ’ t magically turn you from newbie to Web pros. It just let your life easier. </li></ul><ul><li>Learn HTML / XHTML. (learn how to struct) </li></ul><ul><li>Learn CSS / Javascript and get your firebug. (CSS2.1/Prototype/Dojo/YUI/Mochkit/jQuery). </li></ul><ul><li>Learn Browser. </li></ul><ul><li>(IE7, IE6, IE5.5, firefox [gecko], opera, Safari) </li></ul><ul><li>Learn SQL (learn how to maintain/transfer data) </li></ul><ul><li>Learn Apache / Http (learn how to scale) </li></ul><ul><li>Learn You ’ re not genius. Learn from other (genius) people. Don ’ t reinvent a WORSE wheel. </li></ul>
    51. 52. Resource: <ul><li> </li></ul><ul><li>The Django Book </li></ul><ul><li>Django Step by Step ( simplified Chinese ) </li></ul><ul><li>拜 Python 教之 Django 光明會支部 </li></ul><ul><li> </li></ul><ul><li>Tim ( 使徒提姆 ) at kalug mailto: [email_address] :P </li></ul>
    52. 53. <ul><ul><ul><li>Thanks! </li></ul></ul></ul>