Creating the config form


Form’s field names must be unique across forms, so you should prefix them with the name of your app.


from djconfig.forms import ConfigForm

class AppConfigForm(ConfigForm):

    myapp_first_key = forms.BooleanField(initial=True, required=False)
    myapp_second_key = forms.IntegerField(initial=20)

Registering the config form


from django.apps import AppConfig

class MyAppConfig(AppConfig):

    name = 'myapp'
    verbose_name = "Myapp"

    def ready(self):
        # ...

    def register_config(self):
        import djconfig
        from .forms import MyConfigForm


Accessing the config

from djconfig import config

if config.myapp_first_key:
    # ...

Accessing the config within templates:

# template.html

# ...

{% if config.myapp_first_key %}
    # ...
{% endif %}

Editing the config values


def config_view(request):
    if not request.user.is_superuser:
        raise Http404

    if request.method == 'POST':
        form = AppConfigForm(data=request.POST)

        if form.is_valid():
            return redirect('/')
        form = AppConfigForm()

    return render(request, 'app/configuration.html', {'form': form, })

Testing helpers

There is a helper similar to django’s @override_settings that can be used in tests.


from djconfig.utils import override_djconfig

@override_djconfig(myapp_first_key="foo", myapp_second_key="bar")
def test_something(self):
    # ...