usage.txt 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. FAQ: Using Django
  2. =================
  3. Why do I get an error about importing DJANGO_SETTINGS_MODULE?
  4. -------------------------------------------------------------
  5. Make sure that:
  6. * The environment variable DJANGO_SETTINGS_MODULE is set to a
  7. fully-qualified Python module (i.e. "mysite.settings").
  8. * Said module is on ``sys.path`` (``import mysite.settings`` should work).
  9. * The module doesn't contain syntax errors (of course).
  10. I can't stand your template language. Do I have to use it?
  11. ----------------------------------------------------------
  12. We happen to think our template engine is the best thing since chunky bacon,
  13. but we recognize that choosing a template language runs close to religion.
  14. There's nothing about Django that requires using the template language, so
  15. if you're attached to ZPT, Cheetah, or whatever, feel free to use those.
  16. Do I have to use your model/database layer?
  17. -------------------------------------------
  18. Nope. Just like the template system, the model/database layer is decoupled from
  19. the rest of the framework.
  20. The one exception is: If you use a different database library, you won't get to
  21. use Django's automatically-generated admin site. That app is coupled to the
  22. Django database layer.
  23. How do I use image and file fields?
  24. -----------------------------------
  25. Using a :class:`~django.db.models.FileField` or an
  26. :class:`~django.db.models.ImageField` in a model takes a few steps:
  27. #. In your settings file, you'll need to define :setting:`MEDIA_ROOT` as
  28. the full path to a directory where you'd like Django to store uploaded
  29. files. (For performance, these files are not stored in the database.)
  30. Define :setting:`MEDIA_URL` as the base public URL of that directory.
  31. Make sure that this directory is writable by the Web server's user
  32. account.
  33. #. Add the :class:`~django.db.models.FileField` or
  34. :class:`~django.db.models.ImageField` to your model, defining the
  35. :attr:`~django.db.models.FileField.upload_to` option to specify a
  36. subdirectory of :setting:`MEDIA_ROOT` to use for uploaded files.
  37. #. All that will be stored in your database is a path to the file
  38. (relative to :setting:`MEDIA_ROOT`). You'll most likely want to use the
  39. convenience :attr:`~django.db.models.fields.files.FieldFile.url` attribute
  40. provided by Django. For example, if your
  41. :class:`~django.db.models.ImageField` is called ``mug_shot``, you can get
  42. the absolute path to your image in a template with
  43. ``{{ object.mug_shot.url }}``.
  44. How do I make a variable available to all my templates?
  45. -------------------------------------------------------
  46. Sometimes your templates just all need the same thing. A common example would
  47. be dynamically-generated menus. At first glance, it seems logical to simply
  48. add a common dictionary to the template context.
  49. The correct solution is to use a ``RequestContext``. Details on how to do this
  50. are here: :ref:`subclassing-context-requestcontext`.