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.