tutorial06.txt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 keep the static
  12. files somewhere your web server can find it. However, in bigger projects --
  13. especially those comprised of multiple apps -- dealing with the multiple sets
  14. of static files provided by each application starts to get tricky.
  15. That's what ``django.contrib.staticfiles`` is for: it collects static files
  16. from each of your applications (and any other places you specify) into a
  17. single location that can easily be served in production.
  18. .. admonition:: Where to get help:
  19. If you're having trouble going through this tutorial, please head over to
  20. the :doc:`Getting Help</faq/help>` section of the FAQ.
  21. Customize your *app's* look and feel
  22. ====================================
  23. First, create a directory called ``static`` in your ``polls`` directory. Django
  24. will look for static files there, similarly to how Django finds templates
  25. inside ``polls/templates/``.
  26. Django's :setting:`STATICFILES_FINDERS` setting contains a list
  27. of finders that know how to discover static files from various
  28. sources. One of the defaults is ``AppDirectoriesFinder`` which
  29. looks for a "static" subdirectory in each of the
  30. :setting:`INSTALLED_APPS`, like the one in ``polls`` we just created. The admin
  31. site uses the same directory structure for its static files.
  32. Within the ``static`` directory you have just created, create another directory
  33. called ``polls`` and within that create a file called ``style.css``. In other
  34. words, your stylesheet should be at ``polls/static/polls/style.css``. Because
  35. of how the ``AppDirectoriesFinder`` staticfile finder works, you can refer to
  36. this static file in Django as ``polls/style.css``, similar to how you reference
  37. the path for templates.
  38. .. admonition:: Static file namespacing
  39. Just like templates, we *might* be able to get away with putting our static
  40. files directly in ``polls/static`` (rather than creating another ``polls``
  41. subdirectory), but it would actually be a bad idea. Django will choose the
  42. first static file it finds whose name matches, and if you had a static file
  43. with the same name in a *different* application, Django would be unable to
  44. distinguish between them. We need to be able to point Django at the right
  45. one, and the best way to ensure this is by *namespacing* them. That is, by
  46. putting those static files inside *another* directory named for the
  47. application itself.
  48. Put the following code in that stylesheet (``polls/static/polls/style.css``):
  49. .. code-block:: css
  50. :caption: ``polls/static/polls/style.css``
  51. li a {
  52. color: green;
  53. }
  54. Next, add the following at the top of ``polls/templates/polls/index.html``:
  55. .. code-block:: html+django
  56. :caption: ``polls/templates/polls/index.html``
  57. {% load static %}
  58. <link rel="stylesheet" href="{% static 'polls/style.css' %}">
  59. The ``{% static %}`` template tag generates the absolute URL of static files.
  60. That's all you need to do for development.
  61. Start the server (or restart it if it's already running):
  62. .. console::
  63. $ python manage.py runserver
  64. Reload ``http://localhost:8000/polls/`` and you should see that the question
  65. links are green (Django style!) which means that your stylesheet was properly
  66. loaded.
  67. Adding a background-image
  68. =========================
  69. Next, we'll create a subdirectory for images. Create an ``images`` subdirectory
  70. in the ``polls/static/polls/`` directory. Inside this directory, add any image
  71. file that you'd like to use as a background. For the purposes of this tutorial,
  72. we're using a file named ``background.png``, which will have the full path
  73. ``polls/static/polls/images/background.png``.
  74. Then, add a reference to your image in your stylesheet
  75. (``polls/static/polls/style.css``):
  76. .. code-block:: css
  77. :caption: ``polls/static/polls/style.css``
  78. body {
  79. background: white url("images/background.png") no-repeat;
  80. }
  81. Reload ``http://localhost:8000/polls/`` and you should see the background
  82. loaded in the top left of the screen.
  83. .. warning::
  84. The ``{% static %}`` template tag is not available for use in static files
  85. which aren't generated by Django, like your stylesheet. You should always
  86. use **relative paths** to link your static files between each other,
  87. because then you can change :setting:`STATIC_URL` (used by the
  88. :ttag:`static` template tag to generate its URLs) without having to modify
  89. a bunch of paths in your static files as well.
  90. These are the **basics**. For more details on settings and other bits included
  91. with the framework see
  92. :doc:`the static files howto </howto/static-files/index>` and
  93. :doc:`the staticfiles reference </ref/contrib/staticfiles>`. :doc:`Deploying
  94. static files </howto/static-files/deployment>` discusses how to use static
  95. files on a real server.
  96. When you're comfortable with the static files, read :doc:`part 7 of this
  97. tutorial </intro/tutorial07>` to learn how to customize Django's
  98. automatically-generated admin site.