1 / 79

건전지로 달리는 쟝고 세미나

건전지로 달리는 쟝고 세미나. 정재성. Django. Web Framework. CGI.

kaili
Download Presentation

건전지로 달리는 쟝고 세미나

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 건전지로 달리는쟝고 세미나

  2. 정재성

  3. Django

  4. Web Framework

  5. CGI

  6. #!/usr/bin/env pythonimport MySQLdbprint "Content-Type: text/html\n"print "<html><head><title>Books</title></head>"print "<body>"print "<h1>Books</h1>"print "<ul>"connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')cursor = connection.cursor()cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")for row in cursor.fetchall(): print "<li>%s</li>" % row[0]print "</ul>"print "</body></html>"connection.close()

  7. Don’t reinvent the wheel.

  8. MVC

  9. #!/usr/bin/env pythonimport MySQLdbprint "Content-Type: text/html\n"print "<html><head><title>Books</title></head>"print "<body>"print "<h1>Books</h1>"print "<ul>"connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')cursor = connection.cursor()cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")for row in cursor.fetchall(): print "<li>%s</li>" % row[0]print "</ul>"print "</body></html>"connection.close()

  10. models.pyviews.pyurls.pytemplate.html

  11. # models.py (the database tables)from django.db import modelsclass Book(models.Model): name = models.CharField(max_length=50) pub_date = models.DateField()

  12. # views.py (the business logic)from django.shortcuts import render_to_responsefrom models import Bookdef latest_books(request): book_list = Book.objects.order_by('-pub_date')[:10] return render_to_response('latest_books.html', {'book_list': book_list})

  13. # urls.py (the URL configuration)from django.conf.urls.defaults import *import viewsurlpatterns = patterns('', (r'^latest/$', views.latest_books),)

  14. # latest_books.html (the template)<html><head><title>Books</title></head><body><h1>Books</h1><ul>{% for book in book_list %}<li>{{ book.name }}</li>{% endfor %}</ul></body></html>

  15. Loosely coupled

  16. History

  17. Install

  18. Install Python

  19. Install Django

  20. http://www.djangoproject.com/

  21. $ tar xzvfDjango-*.tar.gz $ cdDjango-* $ python setup.py install

  22. Databases코끼리, 나뭇잎돌고래, 11g

  23. DatabasesPostgreSQL, SQLiteMySQL, Oracle

  24. Starting a Project

  25. $ django-admin.py startprojectcourserating

  26. courserating/ __init__.py manage.py settings.py urls.py

  27. Running DEV Server

  28. $ python manage.py runserver

  29. $ python manage.py runserver Validating models... 0 errors found Django version 1.2.1, using settings 'courserating.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CONTROL-C.

  30. Views and URLconfs

  31. First Django-Powered Page

  32. # views.pyfrom django.http import HttpResponsedef hello(request): return HttpResponse("Hello world")

  33. # urls.pyfrom django.conf.urls.defaults import *from views import hellourlpatterns = patterns('', ('^hello/$', hello),)

  34. # settings.py ... ROOT_URLCONF = 'courserating.urls' ...

  35. 1. Request /hello/2. Look at ROOT_URLCONF3. Look at URLpatterns4. Match then call the associated view function5. The view function returns as HttpResponse6. Convert HttpResponse to the proper HTTP response

  36. Dynamic Content

  37. >>> import datetime>>> now = datetime.datetime.now()>>> nowdatetime.datetime(2010, 6, 5, 1, 15, 20, 840166)>>> print now2010-06-05 01:15:20.840166

  38. # views.pyfrom django.http import HttpResponseimport datetimedef current(request): now = datetime.datetime.now() html = "<html><body>Now %s.</body></html>" % now return HttpResponse(html)

  39. # urls.pyfrom django.conf.urls.defaults import *from views import hello, currenturlpatterns = patterns('', ('^hello/$', hello), ('^current/$', current),)

  40. # urls.pyfrom django.conf.urls.defaults import *from views import hello, currenturlpatterns = patterns('', ('^hello/$', hello), (‘^current/$', current), (‘^another_current/$', current),)

  41. Dynamic URLs

  42. # urls.pyurlpatterns = patterns('', ('^time/$', current), ('^time/plus/1/$', one_hour_ahead), ('^time/plus/2/$', two_hours_ahead), ('^time/plus/3/$', three_hours_ahead), ('^time/plus/4/$', four_hours_ahead),)

  43. urlpatterns = patterns('', # ... (r'^time/plus/\d+/$', hours_ahead), # ...)

  44. urlpatterns = patterns('', # ... (r'^time/plus/(\d{1,2})/$', hours_ahead), # ... )

  45. # views.pydef hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt) return HttpResponse(html)

More Related