How do I set my settings.py variables via views and forms?

Hi there,

I want to set my variables in settings.py through the front-end using Views and Forms.

Let’s say, I have following in settings.py:

# My personal settings

NO_OF_DAYS = 0

and I have following in my forms.py:

def class SettingsForm(froms.Form):
    no_of_days = forms.CharField(max_length=254)

Now, I want mixture of views and forms, something like below in my views.py:

from django.conf import settings
from .forms import SettingsForm

class UpdateSettings(View):
    def get(self, request):
        settings_form = SettingsForm()
        return render(request, "settings-template.html", "form": settings_form)

    def post(self, request):
        settings_form = SettingsForm(request.POST)
        if settings_form.is_valid():
            form_data = settings_form.cleaned_data

            settings.NO_OF_DAYS = int(form_data["no_of_days"]) # I WANT SOMETHING LIKE THIS, BUT I WANT SETTINGS.PY TO STORE THE VALUE FOREVER, NOT JUST IN THIS VIEW
 
            return redirect("/success/")

How do I accomplish it?

You don’t. You don’t change settings after startup, and you certainly don’t write to your settings.py file from within your application.

If you need more dynamic settings, you create a model to store those settings, and refer to the values from the model when they’re needed.

1 Like

and refer to the values from the model when they’re needed.

how can I do this? I can’t seem to import models into settings.py

Welcome @adelbordbari !

You are correct, you can’t.

This depends upon the setting and what you’re doing with it.

If you’re talking about fundamental settings such as DATABASES or INSTALLED_APPS, then you can’t. Change to these require the file be changed and the app restarted.

On the other hand, if you have “user-defined” settings that you access elsewhere in your system from django.conf.settings, those should be set as data in a model.

that’s great, appreciate it.

is this solution feasible:

  1. have a model (with key-value fields) to store custom settings
  2. restart the server every time an instance is created/updated/deleted
  3. write the instances as VARIABLE=value into settings.py

do I write the variables into settings.py and then restart or? should I restart the server using management commands? monitor instance changes with signals? i’m kinda lost.

Personally, I would write the new settings file to a different name, then have your restart process copy the new file to the settings file. (Including a check to determine the validity of that file before replacement.)

This depends upon how you’re running your server. If you’ve got your server running by a systemd service, you’d need to issue the systemctl restart command on it.

I’m not sure what you’re asking here. If you’ve got a view that is letting you update settings, then you know when settings have been changed. There’s no need or value to using signals with this.

Are you implying that it is possible to have the user-defined settings in a model and access those model values from django.conf.settings? If that’s the case, could please help with code example of how this can be achieved?

Nope. Not at all.

What I’m saying is that when you need to use those settings in your views, you reference them from the model rather than from django.conf.settings.

Okay. Thanks for the clarification