PageRenderTime 31ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/django-quickstart.txt

https://bitbucket.org/ianb/silverlining/
Plain Text | 250 lines | 187 code | 63 blank | 0 comment | 0 complexity | 1b2b9c1c0330bd9da318c70d1a82bbc4 MD5 | raw file
Possible License(s): GPL-2.0
  1. Django Quickstart
  2. =================
  3. This document describes how to get started with Silver Lining using
  4. Django.
  5. Creating a Layout
  6. -----------------
  7. First thing you have to do (after installing Silver Lining of course)
  8. is create an environment for your new application. Do that like::
  9. $ silver init sampleapp
  10. This creates a directory ``sampleapp/`` with a basic layout. The
  11. first thing we'll do is set up version control for our project.
  12. For the sake of documentation, imagine you go to `bitbucket
  13. <http://bitbucket.org>`_ and create two new repositories, one called
  14. ``sampleapp`` and another called ``sampleapp-lib`` (and for the
  15. examples we'll use the username ``USER``).
  16. We'll go into our new environment and use these::
  17. $ cd sampleapp
  18. $ hg clone http://bitbucket.org/USER/sampleapp src/sampleapp
  19. $ rm -r lib/python/
  20. $ hg clone http://bitbucket.org/USER/sampleapp-lib lib/python
  21. $ mkdir lib/python/bin/
  22. $ echo "syntax: glob
  23. bin/python*
  24. bin/activate
  25. bin/activate_this.py
  26. bin/pip
  27. bin/easy_install*
  28. " > lib/python/.hgignore
  29. $ mv bin/* lib/python/bin/
  30. $ rmdir bin/
  31. $ ln -s lib/python/bin bin
  32. Now there is a basic layout setup, with all your libraries going into
  33. the ``sampleapp-lib`` repository, and your main application in the
  34. ``sampleapp`` repository.
  35. Next we'll install Django::
  36. $ source bin/activate
  37. $ pip install Django
  38. Then we'll set up a standard Django site::
  39. $ cd src/sampleapp
  40. $ django-admin.py startproject sampleapp
  41. Also we'd like to be able to import this file. It'd be nice if there
  42. was a ``setup.py`` file, and we could run ``pip -e src/sampleapp``,
  43. but ``django-admin.py`` doesn't create that itself. Instead we'll get
  44. that on the import path more manually with a ``.pth`` file::
  45. $ echo "../../src/sampleapp" > lib/python/sampleapp.pth
  46. That files will cause ``../../src/sampleapp`` (relative to the .pth
  47. file itself) to be on ``sys.path``, which means when you run ``from
  48. sampleapp import settings`` (for example) it will know to find it in
  49. ``src/sampleapp/sampleapp/settings.py``. Not all Django projects are
  50. setup this way -- applications are often not in a namespace, for
  51. example. But I think it's simply good practice to keep code
  52. partitioned into namespaces.
  53. Also there's the tricky ``$DJANGO_SETTINGS_MODULE`` that you might
  54. have had problems with before. We'll use the file
  55. ``lib/python/silvercustomize.py`` (which is imported everytime Python is
  56. started) to make sure that is always set::
  57. $ echo "import os
  58. os.environ['DJANGO_SETTINGS_MODULE'] = 'sampleapp.settings'
  59. " > lib/python/silvercustomize.py
  60. Also we have a file ``src/sampleapp/sampleapp/manage.py``, and that
  61. file doesn't work *quite* how we'd like. Instead we'll put a file
  62. into ``bin/manage.py`` that does the same thing::
  63. $ rm sampleapp/manage.py
  64. $ cd ../..
  65. $ echo '#!/usr/bin/env python
  66. from django.core.management import execute_manager
  67. from sampleapp import settings
  68. if __name__ == "__main__":
  69. execute_manager(settings)
  70. ' > bin/manage.py
  71. $ chmod +x bin/manage.py
  72. Now, if you were just using plain Django you'd do something like run
  73. ``python manage.py runserver``. But we'll be using ``silver serve``
  74. instead, which means we have to set up the two other files Silver
  75. Lining needs: ``app.ini`` and the runner. Here's a simple
  76. ``app.ini``::
  77. $ echo '[production]
  78. app_name = sampleapp
  79. runner = src/sampleapp/silver-runner.py
  80. ' > src/sampleapp/silver-app.ini
  81. $ rm app.ini
  82. $ ln -s src/sampleapp/silver-app.ini app.ini
  83. The file *must* be in the "root" of your application, and named
  84. ``app.ini``, but it's good to keep it in version control, so we set it
  85. up with a symlink.
  86. It also refers to a "runner", which is the Python file that loads up
  87. the WSGI application. This looks about the same for any Django
  88. application, and we'll put it in ``src/sampleapp/silver-runner.py``::
  89. $ echo 'import django.core.handlers.wsgi
  90. application = django.core.handlers.wsgi.WSGIHandler()
  91. ' > src/sampleapp/silver-runner.py
  92. Now if you want to run the application, you can::
  93. $ silver serve .
  94. This will load it up on ``http://localhost:8080``, and serve up a
  95. boring page. To do something interesting we'll want to use a
  96. database.
  97. Setting Up A Database
  98. ---------------------
  99. At the moment the only good database to use is PostgreSQL with the
  100. PostGIS extensions. Add this line to ``app.ini``::
  101. service.postgis =
  102. This makes the database "available" to the application. For
  103. development you still have to set it up yourself. You should create a
  104. database ``sampleapp`` on your computer.
  105. Next, we'll need to change ``settings.py`` to use the new database
  106. configuration. Here's the lines that you'll see::
  107. DATABASE_ENGINE = '' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
  108. DATABASE_NAME = '' # Or path to database file if using sqlite3.
  109. DATABASE_USER = '' # Not used with sqlite3.
  110. DATABASE_PASSWORD = '' # Not used with sqlite3.
  111. DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
  112. DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
  113. First add this to the top of the file::
  114. import os
  115. Then you'll change those lines to::
  116. DATABASE_ENGINE = 'postgresql_psycopg2'
  117. DATABASE_NAME = os.environ['CONFIG_PG_DBNAME']
  118. DATABASE_USER = os.environ['CONFIG_PG_USER']
  119. DATABASE_PASSWORD = os.environ['CONFIG_PG_PASSWORD']
  120. DATABASE_HOST = os.environ['CONFIG_PG_HOST']
  121. DATABASE_PORT = ''
  122. Now we can create all the default tables::
  123. $ manage.py syncdb
  124. Creating table auth_permission
  125. Creating table auth_group
  126. Creating table auth_user
  127. Creating table auth_message
  128. Creating table django_content_type
  129. Creating table django_session
  130. Creating table django_site
  131. ...
  132. Now we have an empty project that doesn't do anything. Let's make it
  133. do a little something (this is all really based on `the Django
  134. tutorial <http://docs.djangoproject.com/en/dev/intro/tutorial01/>`_).
  135. ::
  136. $ manage.py startapp polls
  137. Django magically knows to put the code in
  138. ``src/sampleapp/sampleapp/polls/`` -- we'll setup the model in
  139. ``src/sampleapp/sampleapp/polls/models.py``::
  140. from django.db import models
  141. class Poll(models.Model):
  142. question = models.CharField(max_length=200)
  143. pub_date = models.DateTimeField('date published')
  144. class Choice(models.Model):
  145. poll = models.ForeignKey(Poll)
  146. choice = models.CharField(max_length=200)
  147. votes = models.IntegerField()
  148. And activate the application by adding ``'sampleapp.polls'`` to
  149. ``INSTALLED_APPS`` in ``src/sampleapp/sampleapp/settings.py``. Also
  150. add ``'django.contrib.admin'`` to get the admin app in place. Run
  151. ``manage.py syncdb`` to get the tables in place.
  152. You can try ``silver serve .`` and go to ``/admin/`` to login and
  153. see your tables. You might notice all the CSS is broken.
  154. Silver Lining serves static files out of the ``static/`` directory.
  155. You don't actually put ``static`` in the URLs, these files are
  156. available at the top-level (unless you create a ``static/static/``
  157. directory). The best way to put files in there is generally symbolic
  158. links.
  159. For Django admin, do this::
  160. $ cd static
  161. $ ln -s ../lib/python/django/contrib/admin/media admin-media
  162. Now edit ``src/sampleapp/sampleapp/settings.py`` and change
  163. ``ADMIN_MEDIA_PREFIX`` to ``'/admin-media'``.
  164. FIXME: probably some other links should be added.
  165. One *last* little thing you might want to do; replace this line in
  166. settings::
  167. SECRET_KEY = 'ASF#@$@#JFAS#@'
  168. With this::
  169. from silversupport.secret import get_secret
  170. SECRET_KEY = get_secret()
  171. Then you don't have to worry about checking a secret into version
  172. control.
  173. Setting Up Your Deployed Database
  174. ---------------------------------
  175. After you write an app and are ready to deploy it, you just run
  176. ``silver update``. *But*, while you'll get a blank database to
  177. use, it will be empty until you run ``syncdb``. A good way to do that
  178. is::
  179. $ silver run HOSTNAME src/myapp/manage.py syncdb
  180. You can use this for data imports or other management tasks as well.
  181. A more robust mechanism is to use the ``update_fetch`` setting in
  182. ``app.ini``: this is a URL (provided *by* your application) that is
  183. fetched everytime your application is updated. This URL can check the
  184. database, create tables, etc. (In the future I expect this can use
  185. scripts in addition to URLs, at which point you can run ``syncdb``
  186. directly; for now you could also create a URL in your app that calls
  187. ``syncdb`` programmatically.)