usage.txt 2.9 KB

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