DESCRIPTION.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. django-appconf
  2. ==============
  3. .. image:: https://secure.travis-ci.org/jezdez/django-appconf.png?branch=develop
  4. :alt: Build Status
  5. :target: http://travis-ci.org/jezdez/django-appconf
  6. A helper class for handling configuration defaults of packaged Django
  7. apps gracefully.
  8. .. note::
  9. This app precedes Django's own AppConfig_ classes that act as
  10. "objects [to] store metadata for an application" inside Django's
  11. app loading mechanism. In other words, they solve a related but
  12. different use case than django-appconf and can't easily be used
  13. as a replacement. The similarity in name is purely coincidental.
  14. .. _AppConfig: https://docs.djangoproject.com/en/stable/ref/applications/#django.apps.AppConfig
  15. Overview
  16. --------
  17. Say you have an app called ``myapp`` with a few defaults, which you want
  18. to refer to in the app's code without repeating yourself all the time.
  19. ``appconf`` provides a simple class to implement those defaults. Simply add
  20. something like the following code somewhere in your app files::
  21. from appconf import AppConf
  22. class MyAppConf(AppConf):
  23. SETTING_1 = "one"
  24. SETTING_2 = (
  25. "two",
  26. )
  27. .. note::
  28. ``AppConf`` classes depend on being imported during startup of the Django
  29. process. Even though there are multiple modules loaded automatically,
  30. only the ``models`` modules (usually the ``models.py`` file of your
  31. app) are guaranteed to be loaded at startup. Therefore it's recommended
  32. to put your ``AppConf`` subclass(es) there, too.
  33. The settings are initialized with the capitalized app label of where the
  34. setting is located at. E.g. if your ``models.py`` with the ``AppConf`` class
  35. is in the ``myapp`` package, the prefix of the settings will be ``MYAPP``.
  36. You can override the default prefix by specifying a ``prefix`` attribute of
  37. an inner ``Meta`` class::
  38. from appconf import AppConf
  39. class AcmeAppConf(AppConf):
  40. SETTING_1 = "one"
  41. SETTING_2 = (
  42. "two",
  43. )
  44. class Meta:
  45. prefix = 'acme'
  46. The ``MyAppConf`` class will automatically look at Django's global settings
  47. to determine if you've overridden it. For example, adding this to your site's
  48. ``settings.py`` would override ``SETTING_1`` of the above ``MyAppConf``::
  49. ACME_SETTING_1 = "uno"
  50. In case you want to use a different settings object instead of the default
  51. ``'django.conf.settings'``, set the ``holder`` attribute of the inner
  52. ``Meta`` class to a dotted import path::
  53. from appconf import AppConf
  54. class MyAppConf(AppConf):
  55. SETTING_1 = "one"
  56. SETTING_2 = (
  57. "two",
  58. )
  59. class Meta:
  60. prefix = 'acme'
  61. holder = 'acme.conf.settings'
  62. If you ship an ``AppConf`` class with your reusable Django app, it's
  63. recommended to put it in a ``conf.py`` file of your app package and
  64. import ``django.conf.settings`` in it, too::
  65. from django.conf import settings
  66. from appconf import AppConf
  67. class MyAppConf(AppConf):
  68. SETTING_1 = "one"
  69. SETTING_2 = (
  70. "two",
  71. )
  72. In the other files of your app you can easily make sure the settings
  73. are correctly loaded if you import Django's settings object from that
  74. module, e.g. in your app's ``views.py``::
  75. from django.http import HttpResponse
  76. from myapp.conf import settings
  77. def index(request):
  78. text = 'Setting 1 is: %s' % settings.MYAPP_SETTING_1
  79. return HttpResponse(text)