/_res/_org/1.0/source/chapter19.py

https://bitbucket.org/Surgo/the-django-book-japanese/ · Python · 121 lines · 39 code · 34 blank · 48 comment · 3 complexity · 5d8a5e57cdf8781a4f555587d0a45c39 MD5 · raw file

  1. #
  2. # Code snippet 1 #########################################################
  3. #
  4. def user_contacts(request):
  5. user = request.GET['username']
  6. sql = "SELECT * FROM user_contacts WHERE username = '%s';" % username
  7. # execute the SQL here...
  8. #
  9. # Code snippet 2 #########################################################
  10. #
  11. SELECT * FROM user_contacts WHERE username = '' OR 'a' = 'a';
  12. #
  13. # Code snippet 3 #########################################################
  14. #
  15. SELECT * FROM user_contacts WHERE username = ''; DELETE FROM user_contacts WHERE 'a' = 'a';
  16. #
  17. # Code snippet 4 #########################################################
  18. #
  19. foo.get_list(bar__exact="' OR 1=1")
  20. #
  21. # Code snippet 5 #########################################################
  22. #
  23. SELECT * FROM foos WHERE bar = '\' OR 1=1'
  24. #
  25. # Code snippet 6 #########################################################
  26. #
  27. from django.db import connection
  28. def user_contacts(request):
  29. user = request.GET['username']
  30. sql = "SELECT * FROM user_contacts WHERE username = %s;"
  31. cursor = connection.cursor()
  32. cursor.execute(sql, [user])
  33. # ... do something with the results
  34. #
  35. # Code snippet 7 #########################################################
  36. #
  37. def say_hello(request):
  38. name = request.GET.get('name', 'world')
  39. return render_to_response("hello.html", {"name" : name})
  40. #
  41. # Code snippet 8 #########################################################
  42. #
  43. <h1>Hello, {{ name }}!</h1>
  44. #
  45. # Code snippet 9 #########################################################
  46. #
  47. <h1>Hello, Jacob!</h1>
  48. #
  49. # Code snippet 10 #########################################################
  50. #
  51. <h1>Hello, <i>Jacob</i>!</h1>
  52. #
  53. # Code snippet 11 ########################################################
  54. #
  55. <h1>Hello, {{ name|escape }}!</h1>
  56. #
  57. # Code snippet 12 ########################################################
  58. #
  59. To: hardcoded@example.com
  60. Subject: hello
  61. cc: spamvictim@example.com
  62. #
  63. # Code snippet 13 ########################################################
  64. #
  65. def dump_file(request):
  66. filename = request.GET["filename"]
  67. filename = os.path.join(BASE_PATH, filename)
  68. content = open(filename).read()
  69. # ...
  70. #
  71. # Code snippet 14 ########################################################
  72. #
  73. import os
  74. import posixpath
  75. # ...
  76. path = posixpath.normpath(urllib.unquote(path))
  77. newpath = ''
  78. for part in path.split('/'):
  79. if not part:
  80. # strip empty path components
  81. continue
  82. drive, part = os.path.splitdrive(part)
  83. head, part = os.path.split(part)
  84. if part in (os.curdir, os.pardir):
  85. # strip '.' and '..' in path
  86. continue
  87. newpath = os.path.join(newpath, part).replace('\\', '/')