views.txt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. ==============
  2. Built-in Views
  3. ==============
  4. .. module:: django.views
  5. :synopsis: Django's built-in views.
  6. Several of Django's built-in views are documented in
  7. :doc:`/topics/http/views` as well as elsewhere in the documentation.
  8. Serving files in development
  9. ----------------------------
  10. .. function:: static.serve(request, path, document_root, show_indexes=False)
  11. There may be files other than your project's static assets that, for
  12. convenience, you'd like to have Django serve for you in local development.
  13. The :func:`~django.views.static.serve` view can be used to serve any directory
  14. you give it. (This view is **not** hardened for production use and should be
  15. used only as a development aid; you should serve these files in production
  16. using a real front-end webserver).
  17. The most likely example is user-uploaded content in :setting:`MEDIA_ROOT`.
  18. ``django.contrib.staticfiles`` is intended for static assets and has no
  19. built-in handling for user-uploaded files, but you can have Django serve your
  20. :setting:`MEDIA_ROOT` by appending something like this to your URLconf::
  21. from django.conf import settings
  22. # ... the rest of your URLconf goes here ...
  23. if settings.DEBUG:
  24. urlpatterns += patterns('',
  25. url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
  26. 'document_root': settings.MEDIA_ROOT,
  27. }),
  28. )
  29. Note, the snippet assumes your :setting:`MEDIA_URL` has a value of
  30. ``'/media/'``. This will call the :func:`~django.views.static.serve` view,
  31. passing in the path from the URLconf and the (required) ``document_root``
  32. parameter.
  33. Since it can become a bit cumbersome to define this URL pattern, Django
  34. ships with a small URL helper function :func:`~django.conf.urls.static.static`
  35. that takes as parameters the prefix such as :setting:`MEDIA_URL` and a dotted
  36. path to a view, such as ``'django.views.static.serve'``. Any other function
  37. parameter will be transparently passed to the view.