usage.txt 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. .. _faq-usage:
  2. FAQ: Using Django
  3. =================
  4. Why do I get an error about importing DJANGO_SETTINGS_MODULE?
  5. -------------------------------------------------------------
  6. Make sure that:
  7. * The environment variable DJANGO_SETTINGS_MODULE is set to a fully-qualified
  8. 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 (of course).
  11. * If you're using mod_python but *not* using Django's request handler,
  12. you'll need to work around a mod_python bug related to the use of
  13. ``SetEnv``; before you import anything from Django you'll need to do
  14. the following::
  15. os.environ.update(req.subprocess_env)
  16. (where ``req`` is the mod_python request object).
  17. I can't stand your template language. Do I have to use it?
  18. ----------------------------------------------------------
  19. We happen to think our template engine is the best thing since chunky bacon,
  20. but we recognize that choosing a template language runs close to religion.
  21. There's nothing about Django that requires using the template language, so
  22. if you're attached to ZPT, Cheetah, or whatever, feel free to use those.
  23. Do I have to use your model/database layer?
  24. -------------------------------------------
  25. Nope. Just like the template system, the model/database layer is decoupled from
  26. the rest of the framework.
  27. The one exception is: If you use a different database library, you won't get to
  28. use Django's automatically-generated admin site. That app is coupled to the
  29. Django database layer.
  30. How do I use image and file fields?
  31. -----------------------------------
  32. Using a ``FileField`` or an ``ImageField`` in a model takes a few steps:
  33. #. In your settings file, define ``MEDIA_ROOT`` as the full path to
  34. a directory where you'd like Django to store uploaded files. (For
  35. performance, these files are not stored in the database.) Define
  36. ``MEDIA_URL`` as the base public URL of that directory. Make sure that
  37. this directory is writable by the Web server's user account.
  38. #. Add the ``FileField`` or ``ImageField`` to your model, making sure
  39. to define the ``upload_to`` option to tell Django to which subdirectory
  40. of ``MEDIA_ROOT`` it should upload files.
  41. #. All that will be stored in your database is a path to the file
  42. (relative to ``MEDIA_ROOT``). You'll most likely want to use the
  43. convenience ``get_<fieldname>_url`` function provided by Django. For
  44. example, if your ``ImageField`` is called ``mug_shot``, you can get the
  45. absolute URL to your image in a template with
  46. ``{{ object.get_mug_shot_url }}``.