tutorial06.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. =====================================
  2. Writing your first Django app, part 6
  3. =====================================
  4. This tutorial begins where :doc:`Tutorial 5 </intro/tutorial05>` left off.
  5. We've built a tested Web-poll application, and we'll now add a stylesheet and
  6. an image.
  7. Aside from the HTML generated by the server, web applications generally need
  8. to serve additional files — such as images, JavaScript, or CSS — necessary to
  9. render the complete web page. In Django, we refer to these files as "static
  10. files".
  11. For small projects, this isn't a big deal, because you can just keep the
  12. static files somewhere your web server can find it. However, in bigger
  13. projects -- especially those comprised of multiple apps -- dealing with the
  14. multiple sets of static files provided by each application starts to get
  15. tricky.
  16. That's what ``django.contrib.staticfiles`` is for: it collects static files
  17. from each of your applications (and any other places you specify) into a
  18. single location that can easily be served in production.
  19. Customize your *app's* look and feel
  20. ====================================
  21. First, create a directory called ``static`` in your ``polls`` directory. Django
  22. will look for static files there, similarly to how Django finds templates
  23. inside ``polls/templates/``.
  24. Django's :setting:`STATICFILES_FINDERS` setting contains a list
  25. of finders that know how to discover static files from various
  26. sources. One of the defaults is ``AppDirectoriesFinder`` which
  27. looks for a "static" subdirectory in each of the
  28. :setting:`INSTALLED_APPS`, like the one in ``polls`` we just created. The admin
  29. site uses the same directory structure for its static files.
  30. Within the ``static`` directory you have just created, create another directory
  31. called ``polls`` and within that create a file called ``style.css``. In other
  32. words, your stylesheet should be at ``polls/static/polls/style.css``. Because
  33. of how the ``AppDirectoriesFinder`` staticfile finder works, you can refer to
  34. this static file in Django simply as ``polls/style.css``, similar to how you
  35. reference the path for templates.
  36. .. admonition:: Static file namespacing
  37. Just like templates, we *might* be able to get away with putting our static
  38. files directly in ``polls/static`` (rather than creating another ``polls``
  39. subdirectory), but it would actually be a bad idea. Django will choose the
  40. first static file it finds whose name matches, and if you had a static file
  41. with the same name in a *different* application, Django would be unable to
  42. distinguish between them. We need to be able to point Django at the right
  43. one, and the easiest way to ensure this is by *namespacing* them. That is,
  44. by putting those static files inside *another* directory named for the
  45. application itself.
  46. Put the following code in that stylesheet (``polls/static/polls/style.css``):
  47. .. code-block:: css
  48. li a {
  49. color: green;
  50. }
  51. Next, add the following at the top of ``polls/templates/polls/index.html``:
  52. .. code-block:: html+django
  53. {% load staticfiles %}
  54. <link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
  55. ``{% load staticfiles %}`` loads the :ttag:`{% static %} <staticfiles-static>`
  56. template tag from the ``staticfiles`` template library. The ``{% static %}``
  57. template tag generates the absolute URL of the static file.
  58. That's all you need to do for development. Reload
  59. ``http://localhost:8000/polls/`` and you should see that the poll links are
  60. green (Django style!) which means that your stylesheet was properly loaded.
  61. Adding a background-image
  62. =========================
  63. Next, we'll create a subdirectory for images. Create an ``images`` subdirectory
  64. in the ``polls/static/polls/`` directory. Inside this directory, put an image
  65. called ``background.gif``. In other words, put your image in
  66. ``polls/static/polls/images/background.gif``.
  67. Then, add to your stylesheet (``polls/static/polls/style.css``):
  68. .. code-block:: css
  69. body {
  70. background: white url("images/background.gif") no-repeat right bottom;
  71. }
  72. Reload ``http://localhost:8000/polls/`` and you should see the background
  73. loaded in the bottom right of the screen.
  74. .. warning::
  75. Of course the ``{% static %}`` template tag is not available for use in
  76. static files like your stylesheet which aren't generated by Django. You
  77. should always use **relative paths** to link your static files between each
  78. other, because then you can change :setting:`STATIC_URL` (used by the
  79. :ttag:`static` template tag to generate its URLs) without having to modify
  80. a bunch of paths in your static files as well.
  81. These are the **basics**. For more details on settings and other bits included
  82. with the framework see
  83. :doc:`the static files howto </howto/static-files/index>` and the
  84. :doc:`the staticfiles reference </ref/contrib/staticfiles>`. :doc:`Deploying
  85. static files </howto/static-files/deployment>` discusses how to use static
  86. files on a real server.
  87. What's next?
  88. ============
  89. The beginner tutorial ends here for the time being. In the meantime, you might
  90. want to check out some pointers on :doc:`where to go from here
  91. </intro/whatsnext>`.
  92. If you are familiar with Python packaging and interested in learning how to
  93. turn polls into a "reusable app", check out :doc:`Advanced tutorial: How to
  94. write reusable apps</intro/reusable-apps>`.